2

我正在尝试学习 google api,我想使用标记聚类器。我从这里创建一个数据库 并按照谷歌所说的去做。我的代码是

`

<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);

`

当我第一次打开它时效果很好,但是当我放大或缩小时,除了最大缩小之外没有集群。我想不通。感谢您的帮助...

4

1 回答 1

0
  var mc = new MarkerClusterer(map, mcmarkers);

此行不能放在 for 循环内。但在它之后。我犯了完全相同的错误:)。

于 2013-04-19T14:03:27.120 回答