Google Maps MarkerClusterer仅显示一个标记

时间:2015-06-03 16:04:33

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

我的标记是从xml解析器生成的。它的工作和显示例如7个标记,但是当我添加MC时它只显示1个标记。 检查我的js。 也许它的问题在这里? markers.push(marker);

    function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();
    var chicago = new google.maps.LatLng(52.6145, 21.3418);
    var mapOptions = {
        zoom: 6,
        center: chicago
    };
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    directionsDisplay.setMap(map);
    var infoWindow = new google.maps.InfoWindow;

    // Change this depending on the name of your PHP file
    downloadUrl("db/parse_xml.php", 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("address");
            var type = markers[i].getAttribute("type");
            var cover = markers[i].getAttribute("cover");
            var point = new google.maps.LatLng(
                parseFloat(markers[i].getAttribute("lat")),
                parseFloat(markers[i].getAttribute("lng")));
            var html = "<div id='infobox'><img src='" + cover + "'/><b>" + name + "</b><br>" + address + " <br/><input type='button' id='end' onClick=calcRoute() name='" + name + "," + address + "' value='Wyznacz trasę'></div>";
            var icon = customIcons[type] || {};
            var marker = new google.maps.Marker({
                map: map,
                position: point,
                icon: icon.icon,
                shadow: icon.shadow
            });

            bindInfoWindow(marker, map, infoWindow, html);

            document.getElementById('pasekBoczny').innerHTML += '<li class="list-sidebar" ><a href="javascript:myclick(' + i + ')"  >' + name + '</a></li>';
            markers.push(marker);
        }

        var markerCluster = new MarkerClusterer(map, markers);
    });

}

1 个答案:

答案 0 :(得分:0)

markers是一个DOMNodeList,它没有方法push

创建一个数组并使用google.maps.Marker

填充数组
var markers = xml.documentElement.getElementsByTagName("marker"),
    markerArray=[];
for (var i = 0; i < markers.length; i++) {

  /**
    * your code
    **/
  //markers.push(marker);<--error, remove it, currently the script will stop here
  markerArray.push(marker);//add this instead
}
var markerCluster = new MarkerClusterer(map, markerArray);