• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

Google Map help

I have the code below from a web site http://maps.forum.nu/gm_markers_from_db.php, where you can see the working example.

When i use the code, it shows me the table and ask me to insert the data, but does not show the map. i checked google API (of course I changedd it here) and its fine and works with google example
What am i doing wrong
here is the code
<?
$CONFIG{'hostname'} = 'localhost';
$CONFIG{'user'} = 'user';
$CONFIG{'password'} = 'pass';
$CONFIG{'db'} = 'googlemap';
 
$DBH = DB_Connect();
 
// Make sure the table exists
assertTable();
 
// define vars
$desc = '';
$lat = '';
$lon = '';
 
// read POST variables if present
foreach ($_POST as $name => $value) {
	$$name = $value;
}
 
 
// If post variables desc,lat and long are not empty then we're inserting
if ($desc &&  $lat &&  $lon) {
	if (is_numeric($lat) && is_numeric($lon) && is_string($desc)) {
 
		if ($lat < -85 || $lat > 85 || $lon < -180 || $lon > 180) {
			print "You've entered invalid values";
			exit;
		}
 
		$desc = addslashes(substr($desc,0,200));
		$sql = "insert into geoPoints values ( null , $lat, $lon, '$desc' )";
		$result = mysql_query($sql,$DBH) or DBError("LINE: " .__LINE__. " $sql");
		print "Point inserted<br><a href=\"{$_SERVER{'PHP_SELF'}}\">Back to the map</a>\n";
		exit;
	}
}
 
 
// Get the javascript defining the points after inserting.
$jsPointsArray = getPointsJS();
 
 
 
$pageHTML = <<<EOH
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Google Maps JavaScript API - PHP/DB Example</title>
 
	
<script src="/key.js"></script>
<script>
	var scriptTag = '<' + 'script src="http://maps.google.com/maps?file=api&v=2&key='+ABQIbbbbTj0LXgWDrRF8XPPcDENwjhRiBgXElYhSqI5wS6AzNsSiTlopxxS1YzTuGcalsTO8Y34DVppI7RuocA+'">'+'<'+'/script>';
	document.write(scriptTag);
</script>
 
<script type="text/javascript">
var mArray = Array();
var map;
var centerPoint = new GLatLng(40.078071,-101.689453);
 
function load() {
	doLoad();
	$jsPointsArray
	addMarkers();
}
 
function doLoad() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.setCenter(centerPoint, 7);
		map.addControl(new GScaleControl());
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		GEvent.addListener(map, 'click', mapClick);
 
	}
}
 
function addMarkers() {
	if (mArray.length) {
		var bounds = new GLatLngBounds();
		for (n=0 ; n < mArray.length ; n++ ) {
			var mData = mArray[n].split(';');
			var point = new GLatLng(mData[0],mData[1]);
			bounds.extend(point);
			var marker = createMarker(point, mData[2]);
			map.addOverlay(marker);
		}
		map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds)); 
	}
}
 
function createMarker(point, title) {
	var marker = new GMarker(point,{title:title});
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml('<div style="width:250px;">' + title + '<hr>Lat: ' + point.y + '<br>Lon: ' + point.x + '</div>');
	});
	return marker;
}
 
function mapClick(marker, point) {
	if (!marker) {
		oLat = document.getElementById("lat");
		oLat.value = point.y;
		oLon = document.getElementById("lon");
		oLon.value = point.x;
		oDesc = document.getElementById("desc");
		oDesc.value = 'New point';
 
 
	}
}
 
 
 
</script>
</head>
 
 
<body onload="load()" onunload="GUnload()">
 
<div id="msg" style="width: 680px; border: 1px solid gray;font: bold 12px verdana;padding:3px;margin:10px;">
Note: Markers from all different visitors are going into the same database and are displayed together.<br>
They could be separated, but I chose to keep it simple. I'll empty the databse once in a while.
</div>
 
<div id="map" style="width: 700px; height: 500px"></div>
 
 
<div id="formDiv">
<table cellspacing="0" cellpadding="2">
<form method="POST">
	<tr>
		<td colspan="2" align="center">Add a marker</td>
	</tr>
	<tr>
		<td>Description</td>
		<td><input id="desc" name="desc"> Example: 'My marker</td>
	</tr>
	<tr>
		<td>Latitude</td>
		<td><input id="lat" name="lat"> Example: 40.0</td>
	</tr>
	<tr>
		<td>Longitude</td>
		<td><input id="lon" name="lon"> Example: -101.5</td>
	</tr>
	<tr>
		<td colspan="2" align="center"><input type="submit" value="Add Point"></td>
	</tr>
	<tr>
		<td colspan="2" align="center"><a href="/gm_markers_from_db.php.txt">View PHP source</a></td>
	</tr>
</form>
</table>
</div>
 
 
 
 
</body>
 
</html>
 
 
 
EOH;
 
 
print $pageHTML;
 
 
print DoQueriy('Table contents',"select * from geoPoints order by id");
 
 
 
///////////////////////////// Functions ///////////////////////////////////////
 
function getPointsJS() {
	global $DBH;
	$sql = "select * from geoPoints";
	$result = mysql_query($sql,$DBH) or DBError("LINE: " .__LINE__. " $sql");
	$nRows = mysql_num_rows($result);
 
	$javaScript = '';
	if ($nRows) {
		while ($row = mysql_fetch_assoc($result)) {
			$row{'description'} = addslashes($row{'description'});
			$row{'description'} = str_replace(';',',',$row{'description'});
			$javaScript .= "mArray.push('{$row{'lat'}};{$row{'lon'}};{$row{'description'}}')\n";
		}
		
		print "Selected $nRows rows\n";
	}
	else {
		print "No points found in database\n";
	}
	return $javaScript;
}
 
 
 
 
function assertTable() {
	global $DBH;
	// Create table, if it does not already exist
	$createTableSQL = <<<EOS
	CREATE TABLE IF NOT EXISTS geoPoints (
	  id int(11) NOT NULL auto_increment,
	  lat decimal(12,8) NOT NULL,
	  lon decimal(12,8) NOT NULL,
	  description varchar(255) NOT NULL,
	  PRIMARY KEY (id)
	) TYPE=MyISAM;
EOS;
 
	$sql = $createTableSQL;
	$result = mysql_query($sql,$DBH) or DBError("LINE: " .__LINE__. " $sql");
}
 
 
function DBError($sql){
	print "Error: \n" . mysql_error() . "\n";
	print "<hr>\n";
	print "$sql\n";
	print "<hr>\n";
	exit;
}
 
 
 
function DB_Connect() {
	global $CONFIG;
	$DBH = mysql_connect($CONFIG{'hostname'}, $CONFIG{'user'}, $CONFIG{'password'}) or DBError("LINE: " .__LINE__. " Connect");;
 
	$sql = "create database IF NOT EXISTS {$CONFIG{'db'}}";
	$result = mysql_query($sql,$DBH) or DBError("LINE: " .__LINE__. " $sql");
	
	mysql_select_db($CONFIG{'db'}, $DBH) or DBError("LINE: " .__LINE__. " Select DB");;
	return $DBH;
}
 
 
 
function DoQueriy($title,$sql){
	global $DBH;
	$HTML = '';
 
	$result = mysql_query($sql,$DBH) or DBError($sql);
	$colCount = mysql_num_fields($result);
	$rowCount = mysql_num_rows($result);
 
	$HTML .= "<table cellspacing=\"0\" cellpadding=\"0\" border>\n";
	$HTML .= "<tr>\n";
	$HTML .= "	<td colspan=\"$colCount\" style=\"padding:3px;font-weight:bold;text-align:center;\">\n";
	$HTML .= "	$title\n";
	$HTML .= "	</td>\n";
	$HTML .= "</tr>\n";
 
	if ($rowCount) {
		if ($row = mysql_fetch_assoc($result)) {
			$HTML .= "<tr>\n";
			foreach ($row as $name => $value) {
				$HTML .= "	<td style=\"padding:3px;\">\n";
				$HTML .=		$name;
				$HTML .= "	</td>\n";
			}		
			$HTML .= "</tr>\n";
		}
		
		mysql_data_seek($result,0);
		
		while ($row = mysql_fetch_assoc($result)) {
			$HTML .= "<tr>\n";
			foreach ($row as $name => $value) {
				$HTML .= "	<td style=\"padding:3px;\">\n";
				$HTML .=		$value;
				$HTML .= "	</td>\n";
			}		
			$HTML .= "</tr>\n";
		}
	}
	else {
		$HTML .= "<tr>\n";
		$HTML .= "	<td colspan=\"$colCount\" style=\"padding:3px;font-weight:bold;text-align:center;\">\n";
		$HTML .= "	Empty\n";
		$HTML .= "	</td>\n";
		$HTML .= "</tr>\n";
	}
	
	$HTML .= "</table>\n";
	$HTML .= "<br>\n";
	return $HTML;
 
}
 
 
?>

Open in new window

0
syedasimmeesaq
Asked:
syedasimmeesaq
  • 5
  • 5
1 Solution
 
Loganathan NatarajanLAMP DeveloperCommented:
to display the map for your site/domain... did you register the domain/site?
0
 
Loganathan NatarajanLAMP DeveloperCommented:
it is important to display the map ..,
0
 
Loganathan NatarajanLAMP DeveloperCommented:
First sign up  & get the key for your site

http://code.google.com/apis/maps/signup.html

then, you have to get the KEY for your site /domain, somethink like this,

Your key is:

ABQIAAAAthb6xeXgSavP92h-9G1pfxQsn9EHL4seOZGF_MH3QESFuR4nmBRZRXq0qbh1Vf5jFUKsRiSLLpTKxQThis key is good for all URLs in this directory:

http://test-URL.com/gmp/Here is an example web page to get you started on your way to mapping glory:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Google Maps JavaScript API Example</title>
    <script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAthb6xeXgSavP92h-9G1pfxQsn9EHL4seOZGF_MH3QESFuR4nmBRZRXq0qbh1Vf5jFUKsRiSLLpTKxQ"
      type="text/javascript"></script>
    <script type="text/javascript">
    //<![CDATA[
    function load() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(37.4419, -122.1419), 13);
      }
    }
    //]]>
    </script>
  </head>
  <body onload="load()" onunload="GUnload()">
    <div id="map" style="width: 500px; height: 300px"></div>
  </body>
</html>
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
syedasimmeesaqAuthor Commented:
I think you get the key only when you regster. Isn't it. Now I had the key, but it wasn't displaying the above code that I put in snippet. It displays the test maps by google fine.

Thanks
0
 
syedasimmeesaqAuthor Commented:
I am pretty sure that something is wrong with my above code and was hoping some expert will see whats wrong there.
Thanks
0
 
syedasimmeesaqAuthor Commented:
any idea?
0
 
syedasimmeesaqAuthor Commented:
ok I got the map showing up using this code below. Now only the last thing I want is some how just show the drop down menu and the user select a city and it shows the mark for only that city.. right now its showing marks for every single thing in database.
Thanks

<html>
<head>
<title>Develop</title>
<script src="http://maps.google.com/maps?file=api&v=2&key=A5QIAAAATjreLXgWDrRF8XPcGEMwjhShwzBVeT4JMjZcXWYv7cJRtxOejRRZ1OaoDadfEWRjjQMFEYVMtBnISA" type="text/javascript"></script>
</head>
<body>
<p><strong>Who-locations in London</strong></p>
<div id="map" style="width: 800px; height: 600px"></div> 
 
<script type="text/javascript">
//<![CDATA[
 
var map = new GMap2(document.getElementById("map"));
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
map.addControl(new GScaleControl());
map.setCenter(new GLatLng(39.577658, -104.988276), 11, G_NORMAL_MAP);
 
// Creates a marker whose info window displays the given number
function createMarker(point, number)
{
var marker = new GMarker(point);
// Show this markers index in the info window when it is clicked
var html = number;
GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html);});
return marker;
};
 
<?php
$link = mysql_connect("localhost", "user", "pass") or die("Could not connect: " . mysql_error());
mysql_selectdb("db",$link) or die ("Can\'t use dbmapserver : " . mysql_error());
 
$result = mysql_query("SELECT * FROM locations",$link);
if (!$result)
{
echo "no results ";
}
while($row = mysql_fetch_array($result))
{
echo "var point = new GLatLng(" . $row['lat'] . "," . $row['lng'] . ");\n";
echo "var marker = createMarker(point, '" . addslashes($row['name']) . "');\n";
echo "map.addOverlay(marker);\n";
echo "\n";
}
 
mysql_close($link);
?>
 
//]]>
</script>
 
</body>
</html>

Open in new window

0
 
syedasimmeesaqAuthor Commented:
why wouldn't this work $result = mysql_query("SELECT * FROM locations where city=XYZ",$link);
0
 
Loganathan NatarajanLAMP DeveloperCommented:
it is always good practice that, count the number of rows in the results set... simply

checking not good
 
if $result = mysql_query("SELECT * FROM locations",$link);
if (!$result)
{
echo "";

}


so, correct like this
$result = mysql_query("SELECT * FROM locations",$link);
$row_count = mysql_num_rows($results);
if ($row_count < 0)
{
echo "no results ";
}
else
{
while($row = mysql_fetch_array($result))
{
echo "var point = new GLatLng(" . $row['lat'] . "," . $row['lng'] . ");\n";
echo "var marker = createMarker(point, '" . addslashes($row['name']) . "');\n";
echo "map.addOverlay(marker);\n";
echo "\n";
}
 
} 
mysql_close($link);

Open in new window

0
 
Loganathan NatarajanLAMP DeveloperCommented:
thanks for your points...
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now