缩放时,marker clusterer不会聚类

时间:2013-04-02 07:34:09

标签: api maps

我正在尝试学习google api,我想使用marker clusterer。我创建了一个数据库 from here并做谷歌所说的任何事情。我的代码是

`

<script src="markerclusterer.js" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
var customIcons = {
  restaurant: {
    icon: 'images/pin1.png',
    shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
  },
  bar: {
    icon: 'images/pin2.png',
    shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
  }
};

function initialize() {
    var markers = null;
    var mcmarkers = [];
  var map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng(47.6145, -122.3418),
    zoom: 12,
    mapTypeId: 'satellite'
  });

  var infoWindow = new google.maps.InfoWindow;

  // Change this depending on the name of your PHP file
  downloadUrl("phpsqlajax_genxml.php", function(data) {
    var xml = data.responseXML;
    markers = xml.documentElement.getElementsByTagName("marker");

    for (var i = 0; i < markers.length; i++) {
      var name = markers[i].getAttribute("name");
      var address = markers[i].getAttribute("address");
      var type = markers[i].getAttribute("type");
      var lat = parseFloat(markers[i].getAttribute("lat"));
      var lng = parseFloat(markers[i].getAttribute("lng"))
      var point = new google.maps.LatLng(lat, lng);
      var html = "<b>" + name + "</b> <br/>" + address;
      var icon = customIcons[type] || {};
      var marker = new google.maps.Marker({
        map: map,
        position: point,
        icon: icon.icon,
        shadow: icon.shadow
      });
      mcmarkers.push(marker);
      var mc = new MarkerClusterer(map, mcmarkers);
      bindInfoWindow(marker, map, infoWindow, html);

    }
  });

}

function bindInfoWindow(marker, map, infoWindow, html) {
  google.maps.event.addListener(marker, 'click', function() {
    infoWindow.setContent(html);
    infoWindow.open(map, marker);
  });
}

function downloadUrl(url, callback) {
  var request = window.ActiveXObject ?
      new ActiveXObject('Microsoft.XMLHTTP') :
      new XMLHttpRequest;

  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      request.onreadystatechange = doNothing;
      callback(request, request.status);
    }
  };

  request.open('POST', url, true);
  request.send(null);
}

function doNothing() {}

//]]>
google.maps.event.addDomListener(window, 'load', initialize);

       

`

当我第一次打开时效果很好但是当我放大或缩小时除了最大缩小外没有任何簇。我无法理解。谢谢你的帮助...

1 个答案:

答案 0 :(得分:0)

  var mc = new MarkerClusterer(map, mcmarkers);

此行不能放在for循环中。但在它之后。我做了同样的错误:)。