Adding markers to a map

suggest change

To add markers to a Google Map, for example from an ArrayList of MyLocation Objects, we can do it this way.

The MyLocation holder class:

public class MyLocation {
  LatLng latLng;
  String title;
  String snippet;
}

Here is a method that would take a list of MyLocation Objects and place a Marker for each one:

private void LocationsLoaded(List<MyLocation> locations){
 
 for (MyLocation myLoc : locations){
    mMap.addMarker(new MarkerOptions()
     .position(myLoc.latLng)
     .title(myLoc.title)
     .snippet(myLoc.snippet)
     .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
 }
}

Note: For the purpose of this example, mMap is a class member variable of the Activity, where we’ve assigned it to the map reference received in the onMapReady() override.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents