Google Maps JavaScript API: How to remove individual marker?

余生长醉 提交于 2020-01-17 12:24:39

问题


I'm using the Google Maps v3 library, with the Multi-Marker library which extends the functionality of Google Maps for super fast adding of map marker icons to a map.

I can't figure out how to remove a single, individual map marker using the multi-marker library linked above.

Does anyone have any ideas how I'd do this using the multi-marker library (and/or Google Maps)? I've tried contacting the lead developer of the project but am not getting a response.

Thanks for any help.

Also, linked is more information on this library

http://blog.redfin.com/devblog/2010/07/introducing_multimarker_the_fastest_way_to_add_many_hundreds_or_thousands_of_markers_on_google_maps.html

UPDATE:

I have linked example code of what I'm doing. I want to dynamically remove specific map marker icons (overlays) but am struggling on how to do that. Any advise would be very appreciated. Thanks

Live example:

http://multimarker.googlecode.com/svn/trunk/fast-marker-overlay/maps-v3/example/clickable.html

回答1:


Normaly, using only the google maps api, to remove an overlay from the map you would need to call the setMap(null) method on the overlay.

As I can see, the Multi-Marker library uses an array to hold all the markers and creates an overlay to show on the map, an overlay that contains the markers. To remove one, it should work to remove the marker from the array (you need to know its position in the array) and redraw the overlay.

Edit:

You need something similar to function clearOverlays() { var i = overlays.length; while (i--) { var overlay = overlays[i]; if (overlay) overlay.setMap(null); delete overlays[i]; } }

But you'll need to know the position in the array of the marker you want to delete. The function will look like this:

 function clearOneOverlay(var position) { var overlay = overlays[position]; if (overlay) overlay.setMap(null); delete overlays[position]; }



回答2:


try this: remember which marker you click and then remove it later

consider following code(Google Maps v2)

var current_marker;

GEvent.addListener(marker, "click", function() {
          current_marker = marker;
      });

//remove later
current_marker.setMap(null);


来源:https://stackoverflow.com/questions/4159505/google-maps-javascript-api-how-to-remove-individual-marker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!