【问题标题】:Google Maps v3 API MarkerCluster - need to wrap dynamic addMarkers in marker varGoogle Maps v3 API MarkerCluster - 需要将动态 addMarkers 包装在标记变量中
【发布时间】:2012-01-31 18:30:07
【问题描述】:

我有一个 v3 谷歌地图可以很好地使用动态标记,但我正在尝试添加 MarkerCluster 功能,如谷歌文档中所述: http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/docs/examples.html

<script type="text/javascript">

 var center = null;
var image = new google.maps.MarkerImage(
'combined.png',
 new google.maps.Size(31,25),
 new google.maps.Point(0,0),
 new google.maps.Point(16,25)
);


        var map = null;
        var currentPopup;
        var bounds = new google.maps.LatLngBounds();
        function addMarker(lat, lng, info) {
            var pt = new google.maps.LatLng(lat, lng);
            bounds.extend(pt);
            var marker = new google.maps.Marker({
                position: pt,
               icon: image,
                map: map
            });
            var popup = new google.maps.InfoWindow({
                content: info,
                maxWidth: 300
            });
            google.maps.event.addListener(marker, "click", function() {
                if (currentPopup != null) {
                    currentPopup.close();
                    currentPopup = null;
                }
                popup.open(map, marker);
                currentPopup = popup;
            });
            google.maps.event.addListener(popup, "closeclick", function() {
               // map.panTo(center);
                currentPopup = null;
            });
        }
        function initMap() {
            map = new google.maps.Map(document.getElementById("map"),  {
                center: new google.maps.LatLng(0, 0),
                zoom: 4,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                mapTypeControl: true,
                mapTypeControlOptions: {
                style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
                },
                navigationControl: true,
                navigationControlOptions: {
                 style: google.maps.NavigationControlStyle.DEFAULT
                }

            });





var mcOptions = {gridSize: 50, maxZoom: 15};
var markers = [];
<?php
do {
$name=$row_rsCity['PARKNAME']; 
$lat=$row_rsCity['lat'];
$lon=$row_rsCity['lng'];
$desc=$row_rsCity['PARKADDR'];
$city=$row_rsCity['PARKCITY'];
$spaces=$row_rsCity['SPACE_CNT'];
$phone=$row_rsCity['PHONE'];
echo ("addMarker($lat, $lon,'<b>$name</b><br/>$desc<br/>$city , CA<br /><br />Phone:     $phone <br />Space Count: $spaces');\n");
} while ($row_rsCity = mysql_fetch_assoc($rsCity));
?>
var mc = new MarkerClusterer(map, markers, mcOptions);
center = bounds.getCenter();
map.fitBounds(bounds);

}
</script>

我相信关键在于包装 var 标记 = [];在 addMarkers 周围,但是当我这样做时

例子:

var markers = [
<?php
do {
$name=$row_rsCity['PARKNAME']; 
$lat=$row_rsCity['lat'];
$lon=$row_rsCity['lng'];
$desc=$row_rsCity['PARKADDR'];
$city=$row_rsCity['PARKCITY'];
$spaces=$row_rsCity['SPACE_CNT'];
$phone=$row_rsCity['PHONE'];
echo ("addMarker($lat, $lon,'<b>$name</b><br/>$desc<br/>$city , CA<br /><br />Phone:     $phone <br />Space Count: $spaces');\n");
} while ($row_rsCity = mysql_fetch_assoc($rsCity));
?>];

Firebug 返回以下错误:

missing ] after element list

任何人都可以帮助使用正确的语法吗?

【问题讨论】:

    标签: google-maps google-maps-api-3


    【解决方案1】:

    更新您的 addMarker 函数以返回标记并接受地图作为参数:

    function addMarker(map, lat, lng, info) {
      var pt = new google.maps.LatLng(lat, lng);
      bounds.extend(pt);
      var marker = new google.maps.Marker({
        position: pt,
        icon: image,
        map: map
      });
      var popup = new google.maps.InfoWindow({
        content: info,
        maxWidth: 300
      });
      google.maps.event.addListener(marker, "click", function() {
        if (currentPopup != null) {
          currentPopup.close();
          currentPopup = null;
        }
        popup.open(map, marker);
        currentPopup = popup;
      });
      google.maps.event.addListener(popup, "closeclick", function() {
        currentPopup = null;
      });
    
      return marker;
    }
    

    然后将标记创建部分更新为:

    var mcOptions = {gridSize: 50, maxZoom: 15};
    var markers = [];
    <?php
    do {
      $name=$row_rsCity['PARKNAME']; 
      $lat=$row_rsCity['lat'];
      $lon=$row_rsCity['lng'];
      $desc=$row_rsCity['PARKADDR'];
      $city=$row_rsCity['PARKCITY'];
      $spaces=$row_rsCity['SPACE_CNT'];
      $phone=$row_rsCity['PHONE'];
      echo ("markers.push(addMarker(map, $lat, $lon,'<b>$name</b><br/>$desc<br/>$city CA<br /><br />Phone: $phone <br />Space Count: $spaces'));\n");
    } while ($row_rsCity = mysql_fetch_assoc($rsCity));
    ?>
    var mc = new MarkerClusterer(map, markers, mcOptions);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-28
      • 1970-01-01
      • 1970-01-01
      • 2011-12-19
      • 2012-01-25
      • 2012-05-11
      相关资源
      最近更新 更多