我在MySQL中存储了一堆纬度和经度标记,可以使用PHP轻松检索它们。我使用Javascript与Google Maps API v3进行了交互,并在数组中加载了多个标记作为虚拟标记,每个标记都有一个信息窗口。
人们会推荐什么作为将MySQL中的数据处理到与google maps api v3一起使用的javascript对象/数组的最佳方法。
最佳答案
通过使用PHP while循环从DB获取值,并将值存储在变量中,如下所示
$location .= '['. $row['latitude'] . ',' . $row['longitude'] . ',"' . $row['city'] . '","' . $row['state'] . '"],' ;
在Javascript中,将PHP变量分配给Javascript变量,如下所示。
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));
}
上面的代码将提供多个标记以及地图中每个标记的信息框。