Google Maps API v3:如何删除所有标记?

在Google Maps API v3中删除所有标记与v2不同。一种方法是保留标记数组,然后循环遍历并调用.setMap(null)。另一种方法是使用自定义实现,但可能依赖于Google代码,存在潜在风险。注意,API更新可能导致以前的方法失效。
摘要由CSDN通过智能技术生成

本文翻译自:Google Maps API v3: How to remove all markers?

In Google Maps API v2, if I wanted to remove all the map markers, I could simply do: 在Google Maps API v2中,如果我想删除所有地图标记,我可以简单地执行以下操作:

map.clearOverlays();

How do I do this in Google Maps API v3 ? 如何在Google Maps API v3中执行此操作?

Looking at the Reference API , it's unclear to me. 看看Reference API ,我不清楚。


#1楼

参考:https://stackoom.com/question/6Tr9/Google-Maps-API-v-如何删除所有标记


#2楼

for (i in markersArray) {
  markersArray[i].setMap(null);
}

is only working on IE. 只在IE上工作。


for (var i=0; i<markersArray.length; i++) {
  markersArray[i].setMap(null);
}

working on chrome, firefox, ie... 在chrome,firefox上工作,即......


#3楼

To clear of all the overlays including polys, markers, etc... 要清除所有叠加层,包括多边形,标记等...

simply use: 只需使用:

map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);}

Here is a function that I wrote to do it form me on a map application: 这是我在地图应用程序中编写的一个函数:

  function clear_Map() {
    directionsDisplay = new google.maps.DirectionsRenderer();
    //var chicago = new google.maps.LatLng(41.850033, -87.6500523);
    var myOptions = {
        zoom: 8,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: HamptonRoads
    }

    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById("directionsPanel"));
}

#4楼

It seems that there is no such function in V3 yet. 似乎V3中还没有这样的功能。

People suggest to keep references to all markers you have on the map in an array. 人们建议在数组中保留对地图上所有标记的引用。 And then when you want to delete em all, just loop trough the array and call .setMap(null) method on each of the references. 然后当你想要删除em all时,只需循环遍历数组并在每个引用上调用.setMap(null)方法。

See this question for more info/code. 有关详细信息/代码,请参阅此问题。

My version: 我的版本:

google.maps.Map.prototype.markers = new Array();

google.maps.Map.prototype.getMarkers = function() {
    return this.markers
};

google.maps.Map.prototype.clearMarkers = function() {
    for(var i=0; i<this.markers.length; i++){
        this.markers[i].setMap(null);
    }
    this.markers = new Array();
};

google.maps.Marker.prototype._setMap = google.maps.Marker.prototype.setMap;

google.maps.Marker.prototype.setMap = function(map) {
    if (map) {
        map.markers[map.markers.length] = this;
    }
    this._setMap(map);
}

The code is edited version of this code http://www.lootogo.com/googlemapsapi3/markerPlugin.html I removed the need to call addMarker manually. 代码是此代码的编辑版本http://www.lootogo.com/googlemapsapi3/markerPlugin.html我删除了手动调用addMarker的需要。

Pros 优点

  • Doing this way you keep the code compact and in one place (doesn't pollute the namespace). 通过这种方式,您可以将代码保存在一个位置(不会污染命名空间)。
  • You don't have to keep track of the markers yourself anymore you can always find all the markers on the map by calling map.getMarkers() 您不必再自己跟踪标记了,您可以通过调用map.getMarkers()始终在地图上找到所有标记。

Cons 缺点

  • Using prototypes and wrappers like I did now makes my code dependent on Google code, if they make a mayor change in their source this will break. 像我现在一样使用原型和包装器使我的代码依赖于Google代码,如果他们在源代码中进行市长更改,这将会破坏。
  • If you don't understand it then you won't be able to fix it if does break. 如果你不理解它,那么如果它破坏了你将无法解决它。 The chances are low that they're going to change anything which will break this, but still.. 很有可能他们会改变任何会打破这个的东西,但仍然......
  • If you remove one marker manually, it's reference will still be in markers array. 如果手动删除一个标记,它的引用仍将位于标记数组中。 (You could edit my setMap method to fix it, but at the cost of looping trough markers array and removing the reference) (您可以编辑我的setMap方法来修复它,但代价是循环槽标记数组并删除引用)

#5楼

google.maps.Map.prototype.markers = new Array();

google.maps.Map.prototype.addMarker = function(marker) {
    this.markers[this.markers.length] = marker;
};

google.maps.Map.prototype.getMarkers = function() {
    return this.markers
};

google.maps.Map.prototype.clearMarkers = function() {
    for(var i=0; i<this.markers.length; i++){
        this.markers[i].setMap(null);
    }
    this.markers = new Array();
};

I don't think there is one in V3 so I used the above custom implementation. 我不认为V3中有一个,所以我使用了上面的自定义实现。

Disclaimer: I did not write this code but I forgot to retain a reference when I merged it into my codebase so I don't know where it came from. 免责声明:我没有写这段代码,但是当我把它合并到我的代码库中时,我忘了保留一个引用,所以我不知道它来自哪里。


#6楼

The " set_map " function posted in both answers appears to no longer work in Google Maps v3 API. 两个答案中发布的“ set_map ”功能似乎在Google Maps v3 API中不再有效。

I wonder what happened 我不知道发生了什么

Update: 更新:

It appears Google changed their API such that " set_map " is not " setMap ". 谷歌似乎改变了他们的API,使得“ set_map ”不是“ setMap ”。

http://code.google.com/apis/maps/documentation/v3/reference.html http://code.google.com/apis/maps/documentation/v3/reference.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值