0

我有一个非常简单的标记聚类示例,但它不起作用(我已经评估了论坛中的许多帖子)。我的问题是 13 个标记在地图上,但无法对它们进行聚类。

在这里,您会找到我的 javascript 代码。我按照这个例子:http: //google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/docs/examples.html

在我的 HTML 中,我同时包含了 markerclusterer.js 和 markerclusterer_compiled.js

先感谢您


函数初始化(){

var myOptions = {
    zoom: 4,
    mapTypeId: google.maps.MapTypeId.ROADMAP
};

var map = new google.maps.Map(document.getElementById('map'), myOptions);

var pos = new google.maps.LatLng(-39.431441,-71.286622);
map.setCenter(pos);

var all = [
    [" -41.8675 ", " -73.827697"],
    [" -23.65 ", " -70.4"],
    [" -42.48014 ", " -73.762414"],
    [" -20.2167 ", " -70.142223"],
    [" -29.906301 ", " -71.250204"],
    [" -39.272254 ", " -71.977629"],
    [" -41.3167 ", " -72.9833"],
    [" -22.908707 ", " -68.199716"],
    [" -33.4625266 ", " -70.6512506"],
    [" -39.819586 ", " -73.24521"],
    [" -33.045646 ", " -71.620361"],
    [" -33.024527 ", " -71.55234"],
    [" -43.1216233 ", " -74.0300122"],
];

for (var i = 0; i < 25; i++) {
        var lat = all[i][0];
        var lng = all[i][1];
        var latLng = new google.maps.LatLng(lat, lng);
        var marker = new google.maps.Marker({map: map, position: latLng,});
        all.push(marker);
}

var markerCluster = new MarkerClusterer(map, all);  

};

4

1 回答 1

0

markercluster 需要一个 google.maps.Marker 对象数组,“all”数组是一个数字数组,您可以在其末尾推送一些 google.maps.Markers。

function initialize() {


var pos = new google.maps.LatLng(-39.431441,-71.286622);

var myOptions = {
    zoom: 4,
    center: pos,
    mapTypeId: google.maps.MapTypeId.ROADMAP
};

var map = new google.maps.Map(document.getElementById('map'), myOptions);

map.setCenter(pos);

var all = [
    [-41.8675, -73.827697],
    [-23.65, -70.4],
    [-42.48014, -73.762414],
    [-20.2167, -70.142223],
    [-29.906301, -71.250204],
    [-39.272254, -71.977629],
    [-41.3167, -72.9833],
    [-22.908707, -68.199716],
    [-33.4625266, -70.6512506],
    [-39.819586, -73.24521],
    [-33.045646, -71.620361],
    [-33.024527, -71.55234],
    [-43.1216233, -74.0300122]
];

var gmarkers = [];
for (var i = 0; i < all.length; i++) {
        var lat = all[i][0];
        var lng = all[i][1];
        var latLng = new google.maps.LatLng(lat, lng);
        var marker = new google.maps.Marker({map: map, position: latLng,});
        gmarkers.push(marker);
}

var markerCluster = new MarkerClusterer(map, gmarkers);

};
      google.maps.event.addDomListener(window, 'load', initialize);

工作示例

于 2013-07-02T15:40:00.627 回答