Google Maps API v3: How to remove all markers?

后端 未结 30 3573
悲哀的现实
悲哀的现实 2020-11-22 05:36

In Google Maps API v2, if I wanted to remove all the map markers, I could simply do:

map.clearOverlays();

How do I do this in Google Maps A

30条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 06:07

    Google's Demo Gallery has a demo on how they do it:

    http://code.google.com/apis/maps/documentation/javascript/examples/overlay-remove.html

    You can view the source code to see how they add markers.

    Long story short, they keep the markers in a global array. When clearing/deleting them, they loop through the array and call ".setMap(null)" on the given marker object.

    However, this example shows one 'trick'. "Clear" for this example means removing them from the map but keeping them in the array, which allows the application to quickly re-add them to the map. In a sense, this acts like "hiding" them.

    "Delete" clears the array as well.

提交回复
热议问题