markerCluster没有显示点

时间:2015-01-08 22:27:09

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

我正在尝试在谷歌地图上聚集一些位置。我试图合并来自this question的代码和来自markercluster examples的代码。但无法修复它。这是我的代码:

<div id="map_canvas" style="width: 98%; height: 500px;border:2px solid #ccc;float:left"></div>

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> <script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/src/markerclusterer.js"></script>     
    <script>
function initialize() {
var elevator;
var myOptions = {
    zoom: 2,
    center: new google.maps.LatLng(0, 0),
    mapTypeId: 'terrain'
    };
var map = new google.maps.Map($('#map_canvas')[0], myOptions);
var addresses = ['Argentina, Buenos Aires', 'Argentina, Cordoba, Cordoba Capital','Argentina, Cordoba, Rio Tercero'];
var markers = [];
for (var x = 0; x < addresses.length; x++) {
    $.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address='+addresses[x]+'&sensor=false', null, function (data) {
        var p = data.results[0].geometry.location
        var latlng = new google.maps.LatLng(p.lat, p.lng);
        var marker = new google.maps.Marker({position: latlng });
        markers.push(marker);
    });
    }
var markerCluster = new MarkerClusterer(map, markers);
}
google.maps.event.addDomListener(window, 'load', initialize);
    </script>   

这不是jquery的问题,因为如果我不尝试使用markerCluster,该示例效果很好。所以我对数组传递做错了。

1 个答案:

答案 0 :(得分:4)

$.getJSON以异步方式运行,当您将markers用作MarkerClusterer的参数时,将不会填充markerCluster.addMarker(marker);

使用

markers.push(marker);

而不是

{{1}}