In php, I create a string that is transferred into a javascript array of objects:
<?php
$markers = '';
while($loc = mysql_fetch_assoc($qry)){
$markers .= "{id:'".$loc['id']."', title:'".$loc['city']."', pos: new google.maps.LatLng(".$loc['latitude'].",".$loc['longitude'].")},";
}
?>
In the initialize function of the Google Maps api in javascript:
<script type="text/javascript">
<?php
if($markers){
echo 'var locations = [' . rtrim($markers, ",") . ']';
unset($markers);
}
?>
//...
//add old markers
var markers = new Array();
for (var i=0; i < locations.length; i++)
{
marker_stack[ locations[i].id ] = new google.maps.Marker({
map: map,
position: locations[i].pos,
title: locations[i].title
});
}
//...
</script>
I fill an array with markers, because I may need to delete some of them later. When I do that, I just want to refer to the marker_stack array with the id that I want to delete.
The error message that I get from the above code is in a js file on Google’s server:
Uncaught Error: Ungültiger Wert für Bauunternehmer-Parameter 0: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object] main.js:36
The issue here is that your
pos:property is a string:new google.maps.LatLng(51,0)and you can’t pass a string to a Marker constructor.
The solution is to set
pos_latandpos_lngas numbers and use those in the constructor: