Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

javascript - Google Maps API v3:如何删除所有标记?(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 ,我不清楚。)

  ask by mp_ translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Simply do the following:(只需执行以下操作:)

I. Declare a global variable:(I.声明一个全局变量:)

var markersArray = [];

II.(II。)

Define a function:(定义一个函数:)
function clearOverlays() {
  for (var i = 0; i < markersArray.length; i++ ) {
    markersArray[i].setMap(null);
  }
  markersArray.length = 0;
}

OR(要么)

google.maps.Map.prototype.clearOverlays = function() {
  for (var i = 0; i < markersArray.length; i++ ) {
    markersArray[i].setMap(null);
  }
  markersArray.length = 0;
}

III.(III。)

Push markers in the 'markerArray' before calling the following:(在调用以下内容之前在'markerArray'中推送标记:)
markersArray.push(marker);
google.maps.event.addListener(marker,"click",function(){});

IV.(IV。)

Call the clearOverlays();(调用clearOverlays();) or map.clearOverlays();(或map.clearOverlays();) function wherever required.(在任何需要的地方运作)

That's it!!(而已!!)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...