MarkerClusterer未在地图上显示

时间:2012-10-18 11:05:18

标签: google-maps markerclusterer

您好我尝试将markerclusterer添加到我的谷歌地图但我肯定会错过一些东西,因为标记在地图上但我看不到群集......

这是我的剧本:

//<![CDATA[

  function initialize() {
  var cluster = [];
  var map = new google.maps.Map(document.getElementById("map_canvas"), {
    center: new google.maps.LatLng(<?php echo get_post_meta($find_CODGEO, 'latitude', true); ?>, <?php echo get_post_meta($find_CODGEO, 'longitude', true); ?>),
    zoom: 14,
    mapTypeId: 'roadmap'
  });
  var mcOptions = {gridSize: 10, maxZoom: 15};

  var infoWindow = new google.maps.InfoWindow;

  downloadUrl("/wp-content/themes/codium-extend/search/search_equipements.php?lat=<?php echo get_post_meta($find_CODGEO, 'latitude', true); ?>&lng=<?php echo get_post_meta($find_CODGEO, 'longitude', true); ?>&type=<?php echo $thematiquematch ; ?>&codgeo=<?php echo $CODGEO ; ?>&radius=50", function(data) {
    var xml = data.responseXML;
    var markers = xml.documentElement.getElementsByTagName("marker");
    for (var i = 0; i < markers.length; i++) {
      var name = markers[i].getAttribute("name");
      var address = markers[i].getAttribute("sous_type");
      var type = markers[i].getAttribute("sous_type_img");
      var offsetLat = markers[i].getAttribute("lat");
      var offsetLng = markers[i].getAttribute("lng");
      var point = new google.maps.LatLng(offsetLat, offsetLng);
      var html = "<b>" + name + "</b> <br/>" + address;
      var icon = 'http://images.commune-mairie.fr/maps/' + type + '.png';
      var marker = new google.maps.Marker({
        map: map,
        position: point,
        icon: 'http://images.commune-mairie.fr/maps/' + type + '.png',
        shadow: icon.shadow
      });
      bindInfoWindow(marker, map, infoWindow, html);

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
                    return function() {
                        infowindow.setContent(markers[i].getAttribute("name"));
                        infowindow.open(map, marker);
                    }
                })(marker, i));
      cluster.push(marker);
    }
    var mc = new MarkerClusterer(map,cluster,mcOptions);


  });

}

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('GET', url, true);
  request.send(null);
}

function doNothing() {}


//]]>

此处有一个实例:http://www.commune-mairie.fr/equipements/lyon-69123/感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

你在循环中这样做:

bindInfoWindow(marker, map, infoWindow, html);

          google.maps.event.addListener(marker, 'click', (function(marker, i) {
                        return function() {
                            infowindow.setContent(markers[i].getAttribute("name"));
                            infowindow.open(map, marker);
                        }
                    })(marker, i));

但是你的bindInfoWindow函数看起来像这样:

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

因此,您正在复制标记点击事件监听器。我不确定这会导致MarkerClusterer出现问题,但无论如何你应该整理一下。