谷歌地图V3 dragend监听器将所有标记添加回地图上 [英] Google maps V3 dragend listener adds all markers back onto map

查看:198
本文介绍了谷歌地图V3 dragend监听器将所有标记添加回地图上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用API​​的V3的谷歌地图。它有一个可拖动的标记,然后是静态的其他标记。我为可拖动标记设置了一个dragend侦听器,该标记调用一个名为clear_markers()的函数,如下所示:

  google.maps。 event.addListener(marker_0,dragend,function(){
clear_markers();
});

函数clear_markers()
{
if(markers){
for(var i = 1; i <= markers.length; i ++){
if(typeof markers [i]!==undefined){
markers [i] .setMap(null);
}
}
}
}

原因我在1开始for循环而不是0我的拖动标记是第一个标记,所以我想清除地图中的所有标记,除了这一个。



这里是问题:



如果我调用clear_markers();以任何其他方式,它工作正常,标记从地图中删除,所以这样的工作:

  $('# ()函数(){
clear_markers();
});

当您拖放绿色标记并从dragend侦听器调用它时,它不起作用。标记确实被删除,但是他们立即再次添加。我知道它们会被删除,因为如果我在for循环杀死脚本后在clear_markers()函数中添加了某些内容,标记就会被删除。但是如果脚本被允许继续运行,它们仍然存在,意味着它们已被删除,然后立即重新加载。



我没有调用任何其他代码,所以它看起来像api给我的bug。有没有人有任何想法?



下面是一个工作示例,显示问题:

https://tinker.io/64b68/1

解决方案

删除markerClusterer。它是添加标记回来,你没有使用它。



更新:



由于您需要如果您不想显示标记,您需要将它们从markerClusterer中移除:

  markerCluster.clearMarkers (); 

(但您需要将其设为全局才能使用)


I have a google map using V3 of the API. It has one marker on it which is draggable and then a load of other markers that are static. I have set up a dragend listener for the draggable marker which calls a function called clear_markers() like so:

google.maps.event.addListener(marker_0, "dragend", function() {
    clear_markers();
});

function clear_markers()
{
  if (markers) {
    for (var i = 1; i <= markers.length; i++ ) {
        if(typeof markers[i] !== "undefined") {
            markers[i].setMap(null);
        }
    }
  }
}

The reason I start the for loop at 1 and not 0 is that my draggable marker is the first marker so I want to clear all markers from the map except this one.

Here is the problem:

If I call clear_markers(); in any other way it works fine and the markers are removed from the map, so something like this works:

$('#mybutton').click(function() {
    clear_markers();
});

When you drag and drop the green marker though and it's called from the dragend listener it does not work. The markers do get removed but then they immediately get re added again. I know they do get removed because if I put something in the clear_markers() function just after the for loop that kills the script, the markers get removed. But if the script is allowed to continue they are still there meaning they have been removed and then instantly added back on again.

I'm not calling any other code so it seems like a bug with the api to me. Does anyone have any ideas?

Here is a working example showing the problem:

https://tinker.io/64b68/1

解决方案

Remove the markerClusterer. It is adding the markers back in and you aren't using it.

Update:

Since you need to keep it, if you want the markers to not be displayed, you need to remove them from the markerClusterer:

 markerCluster.clearMarkers();

(but you will need to make it global to use it that way)

这篇关于谷歌地图V3 dragend监听器将所有标记添加回地图上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆