谷歌地图上的多个标记

时间:2016-06-14 08:17:31

标签: google-maps

我正在尝试在地图上显示多个地址,但它只显示从数据库中检索到的第一个地址。这是代码:

 <?php if($places) { foreach 
    ($places->result_array() as $place) { ?>

    if (GBrowserIsCompatible()) {



       var geocoder = new GClientGeocoder();
       geocoder.getLocations("'<?php echo $place['number']?>','<?php echo $place['street']?>', '<?php echo $place['city']?>' ,'<?php echo $place['country']?>'", function (locations) {         
          if (locations.Placemark)
          {
             var north = locations.Placemark[0].ExtendedData.LatLonBox.north;
             var south = locations.Placemark[0].ExtendedData.LatLonBox.south;
             var east  = locations.Placemark[0].ExtendedData.LatLonBox.east;
             var west  = locations.Placemark[0].ExtendedData.LatLonBox.west;

             var bounds = new GLatLngBounds(new GLatLng(south, west), 
                                            new GLatLng(north, east));

             var map = new GMap2(document.getElementById("map_canvas"));

             map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
             map.addOverlay(new GMarker(bounds.getCenter()));
          }
       });
    } <?php }}?>

如何在同一张地图上显示多个标记?

1 个答案:

答案 0 :(得分:0)

你的问题是你在每次迭代时创建一个新的地图实例

你应该将var map = new GMap2(document.getElementById("map_canvas"));移到循环之外(放在它之前)并且它应该可以正常工作