Google Maps API v3 - How to clear overlays?

前端 未结 7 2089
野性不改
野性不改 2020-12-31 00:30

In Google Maps API v2, I was using map.clearOverlays() to remove the marker and draw them again.

How can I do that using Google Maps API v3 ?

Th

7条回答
  •  伪装坚强ぢ
    2020-12-31 01:07

    This is good one:

    http://apitricks.blogspot.com/2010/02/clearoverlays-in-v3.html

    Article in case the link dies:

    clearOverlays() in V3

    There is no clearOverlays() in API v3. Some practices have been presented. I think this is the simpliest so far.

    Push all the overlays in an array when created (as usual). Following code will clear both map and the array:

    while(overlays[0])
    {
      overlays.pop().setMap(null);
    }
    

    pop() method of an array removes the last element of an array, and returns that element. 'while' keeps that happening as long as there are elements in the array. When overlays[0] does not exist anymore, the mission is completed and code will proceed.

提交回复
热议问题