将MarkerClusterer实施到Google Maps API

时间:2013-02-25 21:21:46

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

我知道这已经被很多人介绍过,而且它只是一个简单的调整,以使它工作,但这让它更令人沮丧。

我是谷歌地图API V3和Javascript的新手。我已经设法制作了带有标记的地图,但我想要实现标记聚类功能。

我的代码如下:

     
<script type="text/javascript">
 var markers = [
['Wathegar',  58.443420, -3.247061, '/Projects/Wathegar/', '/images/MapsIcons/operational.png'],
['Wathegar 2',  58.436726, -3.216505, '/Projects/Wathegar_2/', '/images/MapsIcons/consented.png'],
['Sibmister',  58.570869,  -3.429623, '/Projects/Sibmister/', '/images/MapsIcons/planning.png'],
['Humbleburn',  54.851982,  -1.651425, '/Projects/Humbleburn/', '/images/MapsIcons/planning.png'],
['Achlachan',  58.449348, -3.452797, '/Projects/Achlachan/', '/images/MapsIcons/pre-planning.png'],
['Bonwick',   53.955025,  -0.227451, '/Projects/Bonwick/', '/images/MapsIcons/pre-planning.png'],
['Garton',    54.033081,  -0.494127, '/Projects/Garton/', '/images/MapsIcons/pre-planning.png'],
['Hill of Lybster',  58.603081,   -3.679004, '/Projects/Hill-of-Lybster/', '/images/MapsIcons/planning.png'],
['Moota',   54.710442,    -3.331947, '/Projects/Moota/', '/images/MapsIcons/pre-planning.png'],
['Sherburn Stone',  54.770432,   -1.4593797, '/Projects/Sherburn-Stone/', '/images/MapsIcons/pre-planning.png'],
['Spring Brook',  53.498360,    -1.624646, '/Projects/Spring_Brook/', '/images/MapsIcons/pre-planning.png'],
['Sunnyside',   55.387691,  -3.949585, '/Projects/Sunnyside/', '/images/MapsIcons/pre-planning.png'],
['Thacksons Well',  52.955578,   -0.759824, '/Projects/Thacksons_Well/', '/images/MapsIcons/pre-planning.png'],
];
function initializeMaps() {
var latlng = new google.maps.LatLng( 56.2, -2,5);
var myOptions = {
    zoom: 6,
    center:latlng,
    mapTypeId: google.maps.MapTypeId.HYBRID,
    mapTypeControl: false,
    panControl: false,
    zoomControl: true,
  zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL
  },
    streetViewControl: false,
};
var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
var infowindow = new google.maps.InfoWindow(), marker, i;
for (i = 0; i < markers.length; i++) {  
    marker = new google.maps.Marker({
        position: new google.maps.LatLng(markers[i][1], markers[i][2]),
        map: map,
        url:markers[i][3],
        icon:markers[i][4]
});
google.maps.event.addListener(marker, 'click', function() {
window.location.href = this.url;
});
}
}
</script>

我尝试使用this example中使用的格式以及其他一些博客重建它,但我对JS的了解不足以了解我需要做什么。

非常感谢任何帮助。 谢谢!

2 个答案:

答案 0 :(得分:3)

JSON的格式没问题,它对MarkerClusterer没有任何影响。

您缺少的是创建MarkerClusterer的实例并将标记添加到此实例:

var mc = new MarkerClusterer(map),//MarkerClusterer-instance
    marker, i;
for (i = 0; i < markers.length; i++) {  
    marker = new google.maps.Marker({
        position: new google.maps.LatLng(markers[i][1], markers[i][2]),
        map: map,
        url:markers[i][3],
       icon:markers[i][4]
});

google.maps.event.addListener(marker, 'click', function() {
 window.location.href = this.url;
});
mc.addMarker(marker);//add the marker to the MarkerClusterer
}

请注意:链接页面使用MarkerClusterer for Maps-API-V2,V3使用此更新版本: http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/src/markerclusterer_compiled.js

答案 1 :(得分:1)

您也可以将所有标记一次性推送到MarkerClusterer

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

   google.maps.event.addListener(marker, 'click', function() {
    window.location.href = this.url;
   });
   markersArray.push(marker);

}
var mc = new MarkerClusterer(map,markersArray),//MarkerClusterer-instance which add all markers to the MarkerClusterer