【发布时间】:2016-05-26 13:33:01
【问题描述】:
我正在尝试将我的标记添加到 MarkerCluster,我有以下代码,我将每个 Marker 推送到 markers 数组。然后我声明markerCluster 并添加markers 数组和地图。但是我的 MarkersClusterer 从未显示,有人可以建议这是为什么吗?
map = new google.maps.Map($('#map_canvas')[0], myOptions);
infowindow = new google.maps.InfoWindow();
markerCluster = new MarkerClusterer(map, markers);
//do ajax request, add marker on success
jQuery.post(ajaxurl, data, function(response) {
for (key in response) {
if(response[key]["post_code"] === undefined ){
return;
}
(function () {
var markers = [];
var address = response[key]["address"];
$.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address='+response[key]["post_code"]+'&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,
map: map,
content: address
});
markers.push(Marker);
google.maps.event.addListener(Marker, 'click', function () {
infowindow.setContent(this.content);
infowindow.open(map, this);
});
});
})();
};
});
【问题讨论】:
-
在
markerCluster = new MarkerClusterer(map, markers);中是否定义了markers?我没有看到它在任何地方定义。 -
嗨,它的定义就在下面 (function () {
-
它不在同一个范围内,所以它是未定义的......但这不是唯一的错误。你能创造一个小提琴让我玩吗?我已经这样做了很多次,修复很简单,但我需要玩你的小提琴。
-
我有 var markers = [];到全局范围(在“document.ready”之外),但它仍然是未定义的。
标签: jquery google-maps google-maps-api-3