MarkerClusterer没有聚类标记

时间:2013-01-05 21:31:01

标签: javascript asp.net html google-maps google-maps-api-3

由于存储在地点的大量数据/标记,我的地图非常缓慢,所以我试图让markerclusterer工作显示here。位置只是一个c#字符串数组,每个标记都有一个名称,纬度和经度。 由于某种原因,地图仍然有效,但只是显示单个标记,并没有聚集它们,我无法弄清楚原因。任何帮助,将不胜感激。

<script type="text/javascript">

    var locations = [<%=locations%>];

    var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 10,
        center: new google.maps.LatLng(30.2979536, -97.7470835),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var markers = [];

    for (var i = 0; i < locations.length; i++) {
        var marker = new google.maps.Marker({position: new  google.maps.LatLng(locations[i][1], locations[i][2]), map: map });
        markers.push(marker);
    }

  var markerCluster = new MarkerClusterer(markers);
</script>

1 个答案:

答案 0 :(得分:1)

  • 将地图传递给MarkerClusterer

    var markerCluster = new MarkerClusterer(map, markers);
    

proof of concept fiddle

screenshot of resulting map

您可能希望将纬度和经度转换为字符串中的浮点数(使用parseFloat())。

working example with MarkerClusterer

代码段

var locations = [
  [0, 30.2979536, -97.7470835],
  [1, 30.29, -97.747],
  [2, 30.2979536, -97.7470835],
  [3, 30.2979536, -97.7470835]
];

var map = new google.maps.Map(document.getElementById('map'), {
  zoom: 10,
  center: new google.maps.LatLng(30.2979536, -97.7470835),
  mapTypeId: google.maps.MapTypeId.ROADMAP
});

var markers = [];

for (var i = 0; i < locations.length; i++) {
  var marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    map: map
  });
  markers.push(marker);
}

var markerCluster = new MarkerClusterer(map,markers,{imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry&ext=.js"></script>
<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script>
<div id="map"></div>