I have a bunch of latitude & longitude markers stored in MySQL which I can retrieve simply with PHP. I interact with the Google maps API v3 with Javascript and load several markers in an array as a dummy, each one has a infowindow as well.
What would people recommend as the best method of handing the data in MySQL to a javascript object/array for use with the google maps api v3.
Get the values from DB, by using PHP while loop and store the values in a variable as below
$location .= '['. $row['latitude'] . ',' . $row['longitude'] . ',"' . $row['city'] . '","' . $row['state'] . '"],' ;
In Javascript, assign the PHP variable to a Javascript variable as below.
var locations = [<?php echo isset($location)?$location:''; ?>];
var len = locations.length;
for (i = 0; i < len; i++) {
var image = 'GIVE HERE THE PATH OF IMAGE';
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
icon: image
});
google.maps.event.addListener(marker, 'mouseover', (function(marker, i) {
return function() {
infowindow.setContent(content);
infowindow.open(map, marker);
}
})(marker, i));
}
The above code will give multiple markers and as well as infobox for each marker in the map.
If you have a PhP array containing a lot of markers, for example:
Array
(
[0] => Array
(
["lat"]=> <value>
["lng"]=> <value>
["info"]=> <value>
)
[1] => Array
(
["lat"]=> <value>
["lng"]=> <value>
["info"]=> <value>
)
[2] => ...
[3] => ...
)
using JSON is a good solution as :
To convert your array to JSON from PhP:
json_encode($myMarkersArray, JSON_NUMERIC_CHECK)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.