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.
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.
Add a markerThe 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.
override fun onMapReady(googleMap: GoogleMap) { // Add a marker in Sydney, Australia, // and move the map's camera to the same location. val sydney = LatLng(-33.852, 151.211) googleMap.addMarker( MarkerOptions() .position(sydney) .title("Marker in Sydney") ) googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)) }Java
@Override public void onMapReady(GoogleMap googleMap) { // Add a marker in Sydney, Australia, // and move the map's camera to the same location. LatLng sydney = new LatLng(-33.852, 151.211); googleMap.addMarker(new MarkerOptions() .position(sydney) .title("Marker in Sydney")); 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 markerYou can store an arbitrary data object with a marker using Marker.setTag()
, and retrieve the data object using Marker.getTag()
. The sample below shows how you can count the number of times a marker has been clicked using tags:
/** * A demo class that stores and retrieves data objects with each marker. */ class MarkerDemoActivity : AppCompatActivity(), OnMarkerClickListener, OnMapReadyCallback { private val PERTH = LatLng(-31.952854, 115.857342) private val SYDNEY = LatLng(-33.87365, 151.20689) private val BRISBANE = LatLng(-27.47093, 153.0235) private var markerPerth: Marker? = null private var markerSydney: Marker? = null private var markerBrisbane: Marker? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_markers) val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment? mapFragment!!.getMapAsync(this) } /** Called when the map is ready. */ override fun onMapReady(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. */ override fun onMarkerClick(marker: Marker): Boolean { // Retrieve the data from the marker. val clickCount = marker.tag as? Int // Check if a click count was set, then display the click count. clickCount?.let { val newClickCount = it + 1 marker.tag = newClickCount Toast.makeText( this, "${marker.title} has been clicked $newClickCount 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). return false } }Java
/** * A demo class that stores and retrieves data objects with each marker. */ public class MarkerDemoActivity extends AppCompatActivity implements GoogleMap.OnMarkerClickListener, OnMapReadyCallback { private final LatLng PERTH = new LatLng(-31.952854, 115.857342); private final LatLng SYDNEY = new LatLng(-33.87365, 151.20689); private final LatLng BRISBANE = new LatLng(-27.47093, 153.0235); private Marker markerPerth; private Marker markerSydney; private Marker markerBrisbane; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_markers); SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); mapFragment.getMapAsync(this); } /** Called when the map is ready. */ @Override public void onMapReady(GoogleMap map) { // Add some markers to the map, and add a data object to each marker. markerPerth = map.addMarker(new MarkerOptions() .position(PERTH) .title("Perth")); markerPerth.setTag(0); markerSydney = map.addMarker(new MarkerOptions() .position(SYDNEY) .title("Sydney")); markerSydney.setTag(0); markerBrisbane = map.addMarker(new MarkerOptions() .position(BRISBANE) .title("Brisbane")); markerBrisbane.setTag(0); // Set a listener for marker click. map.setOnMarkerClickListener(this); } /** Called when the user clicks a marker. */ @Override public boolean onMarkerClick(final Marker marker) { // Retrieve the data from the marker. Integer clickCount = (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() + " has been clicked " + 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). return false; } }
Here are some examples of scenarios when it's useful to store and retrieve data with markers:
String
with the marker indicating the type.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.
Kotlinval perthLocation = LatLng(-31.90, 115.86) val perth = map.addMarker( MarkerOptions() .position(perthLocation) .draggable(true) )Java
final LatLng perthLocation = new LatLng(-31.90, 115.86); Marker perth = map.addMarker( new MarkerOptions() .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:
LatLng
value for the marker's position on the map. This is the only required property for a Marker
object.
true
if you want to allow the user to move the marker. Defaults to false
.
false
to make the marker invisible. Defaults to true
.
The below snippet creates a simple marker, with the default icon.
Kotlinval melbourneLocation = LatLng(-37.813, 144.962) val melbourne = map.addMarker( MarkerOptions() .position(melbourneLocation) )Java
final LatLng melbourneLocation = new LatLng(-37.813, 144.962); Marker melbourne = map.addMarker( new MarkerOptions() .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 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.
val melbourneLocation = LatLng(-37.813, 144.962) val melbourne = map.addMarker( MarkerOptions() .position(melbourneLocation) .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)) )Java
final LatLng melbourneLocation = new LatLng(-37.813, 144.962); Marker melbourne = map.addMarker( new MarkerOptions() .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.
Kotlinval melbourneLocation = LatLng(-37.813, 144.962) val melbourne = map.addMarker( MarkerOptions() .position(melbourneLocation) .alpha(0.7f) )Java
final LatLng melbourneLocation = new LatLng(-37.813, 144.962); Marker melbourne = map.addMarker(new MarkerOptions() .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 a BitmapDescriptor
, and defined using one of the methods in the BitmapDescriptorFactory
class.
fromAsset(String assetName)
fromBitmap(Bitmap image)
fromFile(String fileName)
fromPath(String absolutePath)
fromResource(int resourceId)
The below snippet creates a marker with a custom icon.
Kotlinval melbourneLocation = LatLng(-37.813, 144.962) val melbourne = map.addMarker( MarkerOptions() .position(melbourneLocation) .title("Melbourne") .snippet("Population: 4,137,400") .icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow)) )Java
final LatLng melbourneLocation = new LatLng(-37.813, 144.962); Marker melbourne = map.addMarker( new MarkerOptions() .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's flat
property to true
.
val perthLocation = LatLng(-31.90, 115.86) val perth = map.addMarker( MarkerOptions() .position(perthLocation) .flat(true) )Java
final LatLng perthLocation = new LatLng(-31.90, 115.86); Marker perth = map.addMarker( new MarkerOptions() .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.
val perthLocation = LatLng(-31.90, 115.86) val perth = map.addMarker( MarkerOptions() .position(perthLocation) .anchor(0.5f, 0.5f) .rotation(90.0f) )Java
final LatLng perthLocation = new LatLng(-31.90, 115.86); Marker perth = map.addMarker( new MarkerOptions() .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 is 0
.
Set the z-index on the marker's options object by calling MarkerOptions.zIndex()
, as shown in the following code snippet:
map.addMarker( MarkerOptions() .position(LatLng(10.0, 10.0)) .title("Marker z1") .zIndex(1.0f) )Java
map.addMarker(new MarkerOptions() .position(new LatLng(10, 10)) .title("Marker z1") .zIndex(1.0f));
You can access the marker's z-index by calling Marker.getZIndex()
, and you can change it by calling Marker.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 about the effect of z-index on click events below.
Handle marker eventsThe 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 eventsYou 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.
Effect of z-index on click 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()
.
MarkerOptions.draggable(boolean)
prior to adding it to the map, or Marker.setDraggable(boolean)
once it has been added to the map.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4