Google Maps Android API v2

Markers

Markers indicate single locations on the map. You can customize your markers by changing the default color, or replacing the marker icon with a custom image. Info windows can provide additional context to a marker.

Introduction

Markers identify locations on the map. The default marker uses a standard icon, common to the Google Maps look and feel. It's possible to change the icon's color, image or anchor point via the API. Markers are objects of type Marker, and are added to the map with the GoogleMap.addMarker(markerOptions) method.

Markers are designed to be interactive. They receive click events by default, and are often used with event listeners to bring up info windows. Setting a marker's draggable property to true allows the user to change the position of the marker. Use a long press to activate the ability to move the marker.

Video: Getting started with markers

This episode of Maps Live covers the basics of adding markers to your map using the Google Maps Android API v2.

Add a marker

The below example demonstrates how to add a marker to a map. The marker is created at coordinates 0,0, and displays the string "Hello world" in an info window when clicked.

private GoogleMap mMap;
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.addMarker(new MarkerOptions()
        .position(new LatLng(0, 0))
        .title("Hello world"));

Make a marker draggable

You can reposition a marker once its been added to the map so long as its draggable property is set to true. Long press the marker to enable dragging. When you take your finger off the screen, the marker will remain in that position.

Markers are not draggable by default. You must explicitly set the marker to be draggable either with MarkerOptions.draggable(boolean) prior to adding it to the map, or Marker.setDraggable(boolean) once it has been added to the map. You can listen for drag events on the marker, as described in Marker drag events.

The below snippet adds a draggable marker at Perth, Australia.

static final LatLng PERTH = new LatLng(-31.90, 115.86);
Marker melbourne = mMap.addMarker(new MarkerOptions()
                          .position(PERTH)
                          .draggable(true));

Customize a marker

Markers may define a custom image to show in place of the default icon. Defining an icon involves setting a number of properties that affect the visual behavior of the marker.

Markers support customization through the following properties:

Position (Required)
The LatLng value for the marker's position on the map. This is the only required property for a Marker object.
Anchor
The point on the image that will be placed at the LatLng position of the marker. This defaults to the middle of the bottom of the image.
Alpha
Sets the opacity of the marker. Defaults to 1.0.
Title
A string that's displayed in the info window when the user taps the marker.
Snippet
Additional text that's displayed below the title.
Icon
A bitmap that's displayed in place of the default marker image.
Draggable
Set to true if you want to allow the user to move the marker. Defaults to false.
Visible
Set to false to make the marker invisible. Defaults to true.
Flat or Billboard orientation
By default, markers are oriented against the screen, and will not rotate or tilt with the camera. Flat markers are oriented against the surface of the earth, and will rotate and tilt with the camera. Both types of markers do not change size based on zoom. Use GroundOverlays if you desire this effect.
Rotation
The orientation of the marker, specified in degrees clockwise. The default position changes if the marker is flat. The default position for a flat marker is north aligned. When the marker is not flat, the default position is pointing up and the rotation is such that the marker is always facing the camera.

The below snippet creates a simple marker, with the default icon.

static final LatLng MELBOURNE = new LatLng(-37.813, 144.962);
Marker melbourne = mMap.addMarker(new MarkerOptions()
                          .position(MELBOURNE));

Customize the marker color

It's possible to customize the color of the default marker image by passing a BitmapDescriptor object to the icon() method. You can use a set of predefined colors in the BitmapDescriptorFactory object, or set a custom marker color with the BitmapDescriptorFactory.defaultMarker(float hue) method. The hue is a value between 0 and 360, representing points on a color wheel.

static final LatLng MELBOURNE = new LatLng(-37.813, 144.962);
Marker melbourne = mMap.addMarker(new MarkerOptions()
                          .position(MELBOURNE)
                          .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

Customize the marker opacity

You can control the opacity of a marker with the MarkerOptions.alpha() method. Alpha should be specified as a float between 0.0 and 1.0, where 0 is fully transparent and 1 is fully opaque.

static final LatLng MELBOURNE = new LatLng(-37.813, 144.962);
Marker melbourne = mMap.addMarker(new MarkerOptions()
                          .position(MELBOURNE)
                          .alpha(0.7f));

Customize the marker image

You can replace the default marker image with a custom marker image, often called an icon. Custom icons are always set as a BitmapDescriptor, and defined using one of four methods in the BitmapDescriptorFactory class.

fromAsset(String assetName)
Creates a custom marker using an image in the assets directory.
fromBitmap (Bitmap image)
Creates a custom marker from a Bitmap image.
fromFile (String path)
Creates a custom icon from a file at the specified path.
fromResource (int resourceId)
Creates a custom marker using an existing resource.

The below snippet creates a marker with a custom icon.

  private static final LatLng MELBOURNE = new LatLng(-37.813, 144.962);
  private Marker melbourne = mMap.addMarker(new MarkerOptions()
                            .position(MELBOURNE)
                            .title("Melbourne")
                            .snippet("Population: 4,137,400")
                            .icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow)));

Flatten a marker

Marker icons are normally drawn with respect to the screen; rotating, tilting or zooming the map will not change the orientation of the marker. You can set the orientation of a marker to be flat against the earth. Markers that are oriented in this way will rotate when the map is rotated, and change perspective when the map is tilted. Flat markers will retain their size when the map is zoomed in or out.

To change the orientation of the marker, set the marker's flat property to true.

static final LatLng PERTH = new LatLng(-31.90, 115.86);
Marker perth = mMap.addMarker(new MarkerOptions()
                          .position(PERTH)
                          .flat(true));

Rotate a marker

You can rotate a marker around its anchor point with the Marker.setRotation() method. The rotation is measured in degrees clockwise from the default position. When the marker is flat on the map, the default position is North. When the marker is not flat, the default position is pointing up and the rotation is such that the marker is always facing the camera.

The below example rotates the marker 90°. Setting the anchor point to 0.5,0.5 causes the marker to be rotated around its center, instead of its base.

static final LatLng PERTH = new LatLng(-31.90, 115.86);
Marker perth = mMap.addMarker(new MarkerOptions()
                          .position(PERTH)
                          .anchor(0.5,0.5)
                          .rotation(90.0));

Video: Animating markers

In this video, Chris Broadfoot and Ankur Kotwal show modern techniques to animate markers.

For more videos like these, visit the Google Maps Developers Live page.

Marker events

The Maps API allows you to listen and respond to marker events. To listen to these events, you must set the corresponding listener on the GoogleMap object to which the markers belong. When the event occurs on one of the markers on the map, the listener's callback will be invoked with the corresponding Marker object passed through as a parameter. To compare this Marker object with your own reference to a Marker object, you must use equals() and not ==.

You can listen to the following events:

Marker click events

You can use an OnMarkerClickListener to listen for click events on the marker. To set this listener on the map, call GoogleMap.setOnMarkerClickListener(OnMarkerClickListener). When a user clicks on a marker, onMarkerClick(Marker) will be called and the marker will be passed through as an argument. This method returns a boolean that indicates whether you have consumed the event (i.e., you want to suppress the default behavior). If it returns false, then the default behavior will occur in addition to your custom behavior. The default behavior for a marker click event is to show its info window (if available) and move the camera such that the marker is centered on the map.

Marker drag events

You can use an OnMarkerDragListener to listen for drag events on a marker. To set this listener on the map, call GoogleMap.setOnMarkerDragListener. To drag a marker, a user must long press on the marker. When the user takes their finger off the screen, the marker will stay in that position. When a marker is dragged, onMarkerDragStart(Marker) is called initially. While the marker is being dragged, onMarkerDrag(Marker) is called constantly. At the end of the drag onMarkerDragEnd(Marker) is called. You can get the position of the marker at any time by calling Marker.getPosition().

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.