简体   繁体   中英

Swiching between two sets of markers with Google Maps

I am using Google Maps Javascript API and in my website I would like to show people and places but not at the same time. The user should be able to switch from first set of people markers to a set of places markers.

I can simply remove all the people markers and then put all the places markers but it seems like there is another way to do the switching using the OverlayView class.

How does it work ?

It might first seem clumsy but markers itself are overlays and removing and displaying them "by the book" is done keeping references to them using array and looping through setting for example marker.setMap(null) I digged out for you docs example which demonstrates well how you need to play with them. I have used them in a past (displaying and removing) never faced real performance issues, its relatively fast.

Key is not to delete them completely until you want, just setting their map null removes them from the map. Always keep them in a array if you need to display them again. For your case I would either made two arrays of markers or just one array where markers has parameter such as marker.myType="people" which you can use to check against when looping and doing things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM