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 windowscan provide additional context to a marker.
Code samples
TheApiDemos repositoryon GitHub includes a sample that demonstrates various marker features:
Kotlin
- MapWithMarker:A simple map with a marker. See the tutorial onadding a map with a marker-kt.
- MarkerDemoActivity:Using markers on a map, including options and listeners
Java
- MapWithMarker:A simple map with a marker. See the tutorial onadding a map with a marker.
- MarkerDemoActivity:Using markers on a map, including options and listeners
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 receiveclick
events by
default, and are often used with event listeners to bring upinfo
windows.Setting a marker'sdraggable
property totrue
allows the user to change the position of the marker. Use a long press to
activate the ability to move the marker.
By default, when a user taps a marker, the map toolbar appears at the bottom right of the map, giving the user quick access to the Google Maps mobile app. You can disable the toolbar. For more information, see the guide to controls.
Getting started with markers
This episode of Maps Live covers the basics of adding markers to your map using the Maps SDK for Android.
Add a marker
The following example demonstrates how to add a marker to a map. The marker is
created at coordinates-33.852,151.211
(Sydney, Australia), and displays the
string 'Marker in Sydney' in an info window when clicked.
Kotlin
overridefunonMapReady(googleMap:GoogleMap){ // Add a marker in Sydney, Australia, // and move the map's camera to the same location. valsydney=LatLng(-33.852,151.211) googleMap.addMarker( MarkerOptions() .position(sydney) .title("MarkerinSydney") ) googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)) }
Java
@Override publicvoidonMapReady(GoogleMapgoogleMap){ // Add a marker in Sydney, Australia, // and move the map's camera to the same location. LatLngsydney=newLatLng(-33.852,151.211); googleMap.addMarker(newMarkerOptions() .position(sydney) .title("MarkerinSydney")); googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); }
Display additional information about a marker
A common requirement is to show additional information about a place or location when the user taps a marker on the map. See the guide to info windows.
Associate data with a marker
You can store an arbitrary data object with a marker usingMarker.setTag()
,
and retrieve the data object usingMarker.getTag()
.The sample below shows how
you can count the number of times a marker has been clicked using tags:
Kotlin
/** * A demo class that stores and retrieves data objects with each marker. */ classMarkerDemoActivity:AppCompatActivity(), OnMarkerClickListener,OnMapReadyCallback{ privatevalPERTH=LatLng(-31.952854,115.857342) privatevalSYDNEY=LatLng(-33.87365,151.20689) privatevalBRISBANE=LatLng(-27.47093,153.0235) privatevarmarkerPerth:Marker?=null privatevarmarkerSydney:Marker?=null privatevarmarkerBrisbane:Marker?=null overridefunonCreate(savedInstanceState:Bundle?){ super.onCreate(savedInstanceState) setContentView(R.layout.activity_markers) valmapFragment= supportFragmentManager.findFragmentById(R.id.map)asSupportMapFragment? mapFragment!!.getMapAsync(this) } /** Called when the map is ready. */ overridefunonMapReady(map:GoogleMap){ // Add some markers to the map, and add a data object to each marker. markerPerth=map.addMarker( MarkerOptions() .position(PERTH) .title("Perth") ) markerPerth?.tag=0 markerSydney=map.addMarker( MarkerOptions() .position(SYDNEY) .title("Sydney") ) markerSydney?.tag=0 markerBrisbane=map.addMarker( MarkerOptions() .position(BRISBANE) .title("Brisbane") ) markerBrisbane?.tag=0 // Set a listener for marker click. map.setOnMarkerClickListener(this) } /** Called when the user clicks a marker. */ overridefunonMarkerClick(marker:Marker):Boolean{ // Retrieve the data from the marker. valclickCount=marker.tagas?Int // Check if a click count was set, then display the click count. clickCount?.let{ valnewClickCount=it+1 marker.tag=newClickCount Toast.makeText( this, "${marker.title}hasbeenclicked$newClickCounttimes.", Toast.LENGTH_SHORT ).show() } // Return false to indicate that we have not consumed the event and that we wish // for the default behavior to occur (which is for the camera to move such that the // marker is centered and for the marker's info window to open, if it has one). returnfalse } }
Java
/** * A demo class that stores and retrieves data objects with each marker. */ publicclassMarkerDemoActivityextendsAppCompatActivityimplements GoogleMap.OnMarkerClickListener, OnMapReadyCallback{ privatefinalLatLngPERTH=newLatLng(-31.952854,115.857342); privatefinalLatLngSYDNEY=newLatLng(-33.87365,151.20689); privatefinalLatLngBRISBANE=newLatLng(-27.47093,153.0235); privateMarkermarkerPerth; privateMarkermarkerSydney; privateMarkermarkerBrisbane; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_markers); SupportMapFragmentmapFragment= (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); mapFragment.getMapAsync(this); } /** Called when the map is ready. */ @Override publicvoidonMapReady(GoogleMapmap){ // Add some markers to the map, and add a data object to each marker. markerPerth=map.addMarker(newMarkerOptions() .position(PERTH) .title("Perth")); markerPerth.setTag(0); markerSydney=map.addMarker(newMarkerOptions() .position(SYDNEY) .title("Sydney")); markerSydney.setTag(0); markerBrisbane=map.addMarker(newMarkerOptions() .position(BRISBANE) .title("Brisbane")); markerBrisbane.setTag(0); // Set a listener for marker click. map.setOnMarkerClickListener(this); } /** Called when the user clicks a marker. */ @Override publicbooleanonMarkerClick(finalMarkermarker){ // Retrieve the data from the marker. IntegerclickCount=(Integer)marker.getTag(); // Check if a click count was set, then display the click count. if(clickCount!=null){ clickCount=clickCount+1; marker.setTag(clickCount); Toast.makeText(this, marker.getTitle()+ "hasbeenclicked"+clickCount+"times.", Toast.LENGTH_SHORT).show(); } // Return false to indicate that we have not consumed the event and that we wish // for the default behavior to occur (which is for the camera to move such that the // marker is centered and for the marker's info window to open, if it has one). returnfalse; } }
Here are some examples of scenarios when it's useful to store and retrieve data with markers:
- Your app may cater for different types of markers, and you want to treat them
differently when the user clicks them. To accomplish this, you can store a
String
with the marker indicating the type. - You may be interfacing with a system that has unique record identifiers, where the markers represent specific records in that system.
- Marker data may indicate a priority to be used when deciding the z-index of a marker.
Make a marker draggable
You can reposition a marker once its been added to the map so long as its
draggable
property is set totrue
.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 withMarkerOptions.draggable(boolean)
prior to adding it to
the map, orMarker.setDraggable(boolean)
once it has been added to the map.
You can listen for drag events on the marker, as described inMarker drag
events.
The below snippet adds a draggable marker at Perth, Australia.
Kotlin
valperthLocation=LatLng(-31.90,115.86) valperth=map.addMarker( MarkerOptions() .position(perthLocation) .draggable(true) )
Java
finalLatLngperthLocation=newLatLng(-31.90,115.86); Markerperth=map.addMarker( newMarkerOptions() .position(perthLocation) .draggable(true));
Customize a marker
This video shows ways of using markers to visualize locations on a map.
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 aMarker
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 tofalse
. - Visible
- Set to
false
to make the marker invisible. Defaults totrue
. - Flat or Billboard orientation
- By default, markers use a billboard orientation, meaning they are drawn oriented against the device's screen rather than against the map's surface. Rotating, tilting, or zooming the map does not change the orientation of the marker. You can set the orientation of a marker to be flat against the earth. Flat markers rotate when the map is rotated, and change perspective when the map is tilted. As with billboard markers, flat markers retain their size when the map is zoomed in or out.
- 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.
Kotlin
valmelbourneLocation=LatLng(-37.813,144.962) valmelbourne=map.addMarker( MarkerOptions() .position(melbourneLocation) )
Java
finalLatLngmelbourneLocation=newLatLng(-37.813,144.962); Markermelbourne=map.addMarker( newMarkerOptions() .position(melbourneLocation));
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 theBitmapDescriptorFactory
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.
Kotlin
valmelbourneLocation=LatLng(-37.813,144.962) valmelbourne=map.addMarker( MarkerOptions() .position(melbourneLocation) .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)) )
Java
finalLatLngmelbourneLocation=newLatLng(-37.813,144.962); Markermelbourne=map.addMarker( newMarkerOptions() .position(melbourneLocation) .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.
Kotlin
valmelbourneLocation=LatLng(-37.813,144.962) valmelbourne=map.addMarker( MarkerOptions() .position(melbourneLocation) .alpha(0.7f) )
Java
finalLatLngmelbourneLocation=newLatLng(-37.813,144.962); Markermelbourne=map.addMarker(newMarkerOptions() .position(melbourneLocation) .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 aBitmapDescriptor
,and
defined using one of the methods in the
BitmapDescriptorFactory
class.
fromAsset(String assetName)
- Creates a custom marker using the name of a Bitmap image in the assets directory.
fromBitmap(Bitmap image)
- Creates a custom marker from a Bitmap image.
fromFile(String fileName)
- Creates a custom icon using the name of a Bitmap image file located in the internal storage.
fromPath(String absolutePath)
- Creates a custom marker from an absolute file path of a Bitmap image.
fromResource(int resourceId)
- Creates a custom marker using the resource ID of a Bitmap image.
The below snippet creates a marker with a custom icon.
Kotlin
valmelbourneLocation=LatLng(-37.813,144.962) valmelbourne=map.addMarker( MarkerOptions() .position(melbourneLocation) .title("Melbourne") .snippet("Population:4,137,400") .icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow)) )
Java
finalLatLngmelbourneLocation=newLatLng(-37.813,144.962); Markermelbourne=map.addMarker( newMarkerOptions() .position(melbourneLocation) .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'sflat
property to
true
.
Kotlin
valperthLocation=LatLng(-31.90,115.86) valperth=map.addMarker( MarkerOptions() .position(perthLocation) .flat(true) )
Java
finalLatLngperthLocation=newLatLng(-31.90,115.86); Markerperth=map.addMarker( newMarkerOptions() .position(perthLocation) .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.
Kotlin
valperthLocation=LatLng(-31.90,115.86) valperth=map.addMarker( MarkerOptions() .position(perthLocation) .anchor(0.5f,0.5f) .rotation(90.0f) )
Java
finalLatLngperthLocation=newLatLng(-31.90,115.86); Markerperth=map.addMarker( newMarkerOptions() .position(perthLocation) .anchor(0.5f,0.5f) .rotation(90.0f));
Marker z-index
The z-index specifies the stack order of this marker, relative to other markers
on the map. A marker with a high z-index is drawn on top of markers with lower
z-indexes. The default z-index value is0
.
Set the z-index on the marker's options object by calling
MarkerOptions.zIndex()
,as shown in the following code snippet:
Kotlin
map.addMarker( MarkerOptions() .position(LatLng(10.0,10.0)) .title("Markerz1") .zIndex(1.0f) )
Java
map.addMarker(newMarkerOptions() .position(newLatLng(10,10)) .title("Markerz1") .zIndex(1.0f));
You can access the marker's z-index by callingMarker.getZIndex()
,and you can
change it by callingMarker.setZIndex()
.
Markers are always drawn above tile layers and other non-marker overlays (ground overlays, polylines, polygons, and other shapes) regardless of the z-index of the other overlays. Markers are effectively considered to be in a separate z-index group compared to other overlays.
Read aboutthe effect of z-index on click eventsbelow.
Handle 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 theGoogleMap
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
correspondingMarker
object passed through as a parameter. To compare this
Marker
object with your own reference to aMarker
object, you must use
equals()
and not==
.
You can listen to the following events:
Marker click events
You can use anOnMarkerClickListener
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 returnsfalse
,then the default behavior will occur in
addition to your custom behavior. The default behavior for a marker click
event is to show itsinfo window(if available) and move the
camera such that the marker is centered on the map.
Effect of z-index on click events:
- When a user clicks on a cluster of markers, the click event is triggered for the marker with the highest z-index.
- At most one event is triggered per click. In other words, the click is not passed down to the markers or other overlays with lower z-index values.
- Clicking on a cluster of markers causes subsequent clicks to cycle through the cluster, selecting each in turn. The order of the cycle first prioritises z-index, then proximity to the click point.
- If the user clicks outside the proximity of the cluster, the API recalculates the cluster and resets the state of the click cycle so that it starts from the beginning.
- The click event falls through marker clusters to other shapes and overlays before restarting the cycle.
- Markers are effectively considered to be in a separate z-index group compared to other overlays or shapes (polylines, polygons, circles, and/or ground overlays), regardless of the z-index of the other overlays. If multiple markers, overlays or shapes are overlaid on top of each other, the click event is cycled through the cluster of markers first, then triggered for other clickable overlays or shapes, based on their z-index values.
Marker drag events
You can use anOnMarkerDragListener
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 callingMarker.getPosition()
.