Google Maps API - 群集问题

时间:2017-11-03 20:09:15

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

我正在尝试将聚类添加到我已经工作的地图中。但是,经过多次尝试后,我无法理解为什么群集不显示。我想知道代码中是否存在冲突,或者群集是否与地图上的infowindows或者侦听器不兼容。如果可能的话,请你查看这段代码,看看你是否注意到任何危险信号。非常感谢并提前感谢您!

    <script src="markerclusterer.js"></script>
<script>
  var customLabel = {
    community: {
      label: 'C'
    },
    building2: {
      label: 'A'
    }
  };

    function initMap() {
    var map = new google.maps.Map(document.getElementById('map'), {
      center: new google.maps.LatLng(40, -90), //This needs to be dynamic based on map location
      zoom: 11
    });
    var infoWindow = new google.maps.InfoWindow;

      // Change this depending on the name of your PHP or XML file
      downloadUrl('all_marker_data.php', function(data) {
        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName('marker');
        Array.prototype.forEach.call(markers, function(markerElem) {
          var id = markerElem.getAttribute('id');
          var name = markerElem.getAttribute('name');
          var address = markerElem.getAttribute('address');
          var type = markerElem.getAttribute('type');
          var dlink = markerElem.getAttribute('dlink');
          var comm_link = markerElem.getAttribute('comm_link');
          var target = markerElem.getAttribute('target');
          var point = new google.maps.LatLng(
              parseFloat(markerElem.getAttribute('lat')),
              parseFloat(markerElem.getAttribute('lng')));    
          var infowincontent = '<div>'+
                               '<h5>'+
                               '<a href="'+
                               comm_link+
                               '"'+
                               'target="'+
                               target+
                               '"'+
                               '>'+                                  
                               name+
                               '</a>'+
                               '</h5>'+
                               '</div>'+
                               '<div>'+                                  
                               '<p>'+
                               address+
                               '</p>'+
                               '</div>'+
                               '<div>'+                                                                      
                               '<a href="'+
                               dlink+
                               '" target="_new">'+
                               'Get Directions >></a>'+
                               '</div>'+
                               '</div>';
          var icon = customLabel[type] || {};
          var marker = new google.maps.Marker({
            map: map,
            position: point,
            label: icon.label
          });
          marker.addListener('click', function() {
            infoWindow.setContent(infowincontent);
            infoWindow.open(map, marker);
          });
        });
      });
      var markerCluster = new MarkerClusterer(map, markers, {
        imagePath: 'images/m'});
    }

  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() {}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=XXX&callback=initMap">
</script>

如果有任何其他信息可以帮助回答这个问题,请告诉我,我可以尽力提供这些信息。谢谢!

1 个答案:

答案 0 :(得分:0)

看起来像是图像问题。 在代码上,您将imagePath指向名为“images”的文件夹,而icons(cluster)必须命名为“m1.png”,“m2.png”,...,“m5.png”。检查是否存在。

此链接可能会有所帮助:https://developers.google.com/maps/documentation/javascript/marker-clustering?hl=es#agregar-un-agrupador-de-marcadores-en-clsteres

问候!