Multiple Markers Using PHP/JavaScript

997 views
Skip to first unread message

MarkF

unread,
Jun 17, 2010, 10:40:28 PM6/17/10
to Google Maps JavaScript API v3
Hi,

I'm trying to load multiple markers to my map using the following
code. It load the multiple markers just fine, but when I click on the
marker to provide info, it gives me the same text (the last text
loaded from the db query) into each InfoWindow! Anybody know how I can
improve my code to fix this? Thanks!

<code>

<script type="text/javascript">
// Creating a LatLng object containing the coordinate for the center
of the map
var latlng = new google.maps.LatLng(32.396989, -111.004427);
// Creating an object literal containing the properties we want to
pass to the map
var options = {
zoom: 10,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
// Calling the constructor, thereby initializing the map
var map = new google.maps.Map(document.getElementById('map'),
options);
<?php
$link = mysql_connect("localhost", "<user>", "<password>") or
die("Could not connect: " . mysql_error());
mysql_selectdb("lightning",$link) or die ("Can\'t use dbmapserver :
" . mysql_error());

$result = mysql_query("SELECT * FROM shotspots",$link);
if (!$result)
{
echo "no results ";
}
while($row = mysql_fetch_array($result))
{
echo "var marker = new google.maps.Marker({\n";
echo " position : new google.maps.LatLng(" . $row['latitude'] .
"," . $row['longitude'] . "),\n";
echo " map : map,\n";
echo " icon : 'images/photo.png'\n";
echo "});\n";
echo "var infowindow = new google.maps.InfoWindow({\n";
echo " content : '" . addslashes($row['description']) . "'\n";
echo "})\n";
echo "google.maps.event.addListener(marker, 'click', function() {\n";
echo " infowindow.open(map, marker);\n";
echo "})\n";
}

mysql_close($link);
?>
</script>

</code>

Chad Killingsworth

unread,
Jun 18, 2010, 1:15:50 AM6/18/10
to Google Maps JavaScript API v3
This is a common issue. Daniel wrote a great post on how to address
it. Take a look at http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/f40390e4a6ba89e9/

Chad Killingsworth
Reply all
Reply to author
Forward
0 new messages