Wednesday, 28 August 2013

Android - Adding and removing markets on Google Maps V2

Unlike the first version of the Google Maps for Android, to add and remove markets in the Google Maps, no overlay is required. Instead, a simple marker could be used.

1. Add a marker to the map
To add a new marker to the map it is so simple as called the method add Marker of the instance of GoogleMaps

private GoogleMap googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(; Marker myMarker = googleMap.addMarker(new MarkerOptions().position(new LatLng(0,0).title("Hello world"));

The object MarkerOptions could be customized easily. The unique required data is the position (Latitude and Longitude), which is set by the method positions(LatLng).

Other options are:
- Title: The title which is show when the user clicks on the marker
- Snippet: A hint of the marker displayed when the user clicks on the marker.
- Draggable: Set to true to allow the user to move the marker. The default option is false.
- Visible: Set the marker visible or not.
- Anchor: The point on the image that will be placed at the LatLng position of the marker. More info here.
- Icon: The image displayed as icon. It could be customized.

Here is an example of usage:

googleMap.addMarker(new MarkerOptions()
    .position(new LatLng(53.551, 9.993)
    .snippet("Kiel is cool")

Source: Voguella - Google Maps Android API v2 - Tutorial

2. Remove the marker
Unfortunately it seems there is not method in the class Google maps to list all the markers. If the app want's to remove a specific marker from the Google Maps, other way must be found.

The good news is that the Marker object itself has an remove() method which could be used to remove it from the Google Maps. In order to know which marker are in the map, when the app adds a new marker in the map, it must be saved.

Marker myMarker = googleMap.addMarker(new MarkerOptions().position(new LatLng(0.0, 0.0)));

3. Clean the map
The object GoogleMap has its own method to clear all the markers in the maps.


For more information:
Android developers - Google maps
Google developers - Google maps Android API v2 Markers

No comments:

Post a Comment