I have a set of coordinates in a database that I want to be used to add markers on a google map
I created the following code
var map;
function initialize()
{
var latloncenter = new google.maps.LatLng(51,-1.4469157);
var myOptions =
{
zoom: 4,
center: latloncenter,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
};
<? while($row = mysql_fetch_assoc($result)){?>
var lon = "<?php echo($row['Longitude']); ?>";
var lat = "<?php echo($row['Latitude']); ?>";
//alert_test(lat,lon);
setmarker(lat,lon);
<? } ?>
function setmarker(lat,lon)
{
var latlongMarker = new google.maps.LatLng(lat,lon);
var marker = new google.maps.Marker
(
{
position: latlongMarker,
map: map,
title:"Hello World!"
}
);
}
function alert_test(lat,lon)
{
alert(lat +" "+ lon);
}
<body onload="initialize()"><div id="map_canvas"></div></body>
it’s loading the map, and I know the query from the database is working correctly, However I think I am doing something wrong in the setmarker() method…. anyone knows how to make it work?
You have to put your PHP-setmarker loop inside
initialize(). Please try it.I don’t know all the intricacies of JavaScript but I think it’s a good idea to write
varonly once invar lat, var lon(it gets repeated in the PHP loop). Well, the JS interpreters probably don’t care either way.