【问题标题】:Unable to add markers to Marker Cluster无法将标记添加到标记簇
【发布时间】: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


【解决方案1】:

您的markers 变量在MarkerClusterer 中使用时定义在范围之外。您将标记添加到仅存在于匿名 (function(){.. 中的不同 markers 中。要了解这个问题,建议你看看thisthis的文章。

编辑

还有!正如用户 vyx.ca 所指出的,MarkerClusterer 具有addMarker() 方法,这是向集群器添加标记的advised way。我的代码现在反映了这种变化。

代码中有注释部分引用了这些更改:

var markers = []; //HERE!!
map = new google.maps.Map($('#map_canvas')[0], myOptions);
infowindow = new google.maps.InfoWindow();
markerCluster = new MarkerClusterer(map, markers);

jQuery.post(ajaxurl, data, function(response) {

    for (key in response) {
        if(response[key]["post_code"] === undefined ){
            return; 
        }
        (function () {
            //REMOVE THIS !! 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
                });
                //Changed to line below - markers.push(Marker);
                markerCluster.addMarker(Marker);
                google.maps.event.addListener(Marker, 'click', function () {    
                    infowindow.setContent(this.content);
                    infowindow.open(map, this);
                });
            });
        })();
    };
});  

【讨论】:

  • 如果这不起作用,请将markers.push(Marker);更改为markerClusterer.addMarker(Marker);
  • 谢谢!这现在有效,我有一个奇怪的问题,没有图标显示为集群(我只得到一个数字)。不过我会调查一下。
  • 看看这个答案:stackoverflow.com/a/37184213/2661226 您可能只需要设置正确的图片网址。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-23
  • 1970-01-01
  • 2012-06-27
  • 2018-10-11
  • 2021-03-21
  • 1970-01-01
相关资源
最近更新 更多