25

I'm trying to get a list Markers(using GMaps V2 for Android) similar to the getOverlays() method I used in GMaps V1.1 for Android here:

private MapView mapView; 

mapView.getOverlays().add(overlay);

How can I do something similar to what I did above but with Markers?

Any help would be appreciated.

user268397
  • 1,917
  • 7
  • 38
  • 56

2 Answers2

54

How can I do something similar to what I did above but with Markers?

When you call addMarker(), save the resulting Marker object in a collection of your choice (e.g., ArrayList<Marker>).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • What about getting the `MyLocation` marker if you called `setMyLocationEnabled(true)`? – theblang Nov 20 '13 at 22:16
  • @mattblang: That is not a `Marker`, and so you cannot retrieve it as one. – CommonsWare Nov 20 '13 at 22:30
  • [This SO answer](http://stackoverflow.com/questions/14828217/android-map-v2-zoom-to-show-all-the-markers) has an easy method for zooming to the correct level to include everything, but I guess if I am wanting to include the `MyLocation` marker then the solution may be a bit more complicated. – theblang Nov 20 '13 at 22:34
  • @mattblang: Theoretically, if you use `LocationClient`, you should be getting the same location data as what the map uses, and so you just add that to the `LatLngBounds.Builder`. – CommonsWare Nov 20 '13 at 23:03
  • I moved this to a [new SO question](http://stackoverflow.com/questions/20109212/zoom-to-the-closest-level-that-still-shows-all-markers-including-the-blue-myloc). – theblang Nov 20 '13 at 23:19
  • What about cluster markers, which aren't created until the user taps a marker and an event is fired? – Nathan M Sep 13 '19 at 21:00
  • @NathanM: I have no idea, sorry. – CommonsWare Sep 13 '19 at 21:07
7

Alternative to the correct answer from CommonsWare would be using Android Maps Extensions, which adds getMarkers() function to GoogleMap.

Btw. mapView.getOverlays().add(overlay) code from Maps V1 is a very bad design choice. You should not give out List for modification. So this will not work with Extensions library.

MaciejGórski
  • 22,187
  • 7
  • 70
  • 94