Markers
Utilize different Maps SDK marker features to provide engaging functionality in your mobile app.
The main marker features are:
- Customizable marker icons: Use the default TomTom marker, or favorite icons, or use your own.
- Simple and decal marker mode: Choose how the marker is rendered on the map.
- Draggable markers: Allow your users to drag and drop a marker around the map.
- Animated markers: Use GIFs as marker icons.
You can find details of each of the marker features following the examples on this page.
Sample use case: In your app, you want to display a number of markers to mark places on the map. Use the code snippet below to display a single marker at specific coordinates on the map.
Examples are shown in the following code snippet:
Icon Marker | Decal Marker |
Create a simple marker with a default icon and add it to the map; only the location needs to be specified:
Marker selected
You can implement an observable marker selected event. You can do that with the global listener OnMarkerClickedListener for all markers that have to be registered on MarkerSettings as shown in the following code snippets:
For that purpose, the passed object has to implement the following interface:
1interface OnMarkerClickListener {2 /**3 * Called when the user clicks on the marker.4 *5 * @param marker Selected marker.6 */7 void onMarkerClick(@NonNull Marker marker);8}
You can remove all markers from the map in this way:
You can remove all the markers one-by-one in this way:
- Remove all markers with the tag "tag".
- Remove marker by id.
Simple and decal marker modes
- Focal (default): Where the icon always stands vertically even when the map is rotated. All nine callout anchor points are supported with this annotation type.
- Decal: Where the icon sticks to the map even when the map is rotated. Only one callout anchor point is supported for decal MarkerAnchor - Bottom.
Mixing icons of different modes is generally unsupported and advised against, as there are unresolvable depth-sorting issues and the results will not always look correct. It is safe to mix some modes, however, such as either of the decal modes with any of the standing modes.
Non-decal icons may optionally cast a shadow on the map. You will need to leave additional empty space between icons in your drawable if you specify a non-zero blur amount.
Draggable markers
Thanks to this feature your users are able move existing markers around the map. They long-click on a marker that is on the map to start dragging it, then they drop the marker on a selected position by releasing the finger from the screen.
In order to do that you need to set 'draggable' to true:
It is also possible to register to receive the Marker’s dragging callbacks.
As an example you can create a listener like this one:
Then register it to receive events related to dragging the marker over the map:
Animated markers
Thanks to this feature you can use GIFs as marker icons. For this purpose, you need to place your images in the assets folder and then use them in the following way:
Marker anchoring
You can change the anchoring point for your icon. The anchor specifies which point of an annotation image is attached to the map. The annotation will rotate around this anchoring point when rotating the map. You can use the default image representation of an annotation which is attached to the bottom height center width as presented in the following figure. You can change this behavior by MarkerAnchor enumeration on the builder. E.g., if you want to use a crosshair annotation image, you should set the anchor to MarkerAnchor.Center
All possible anchors are defined in the enum:
1/**2 * Copyright (c) 2015-2021 TomTom N.V. All rights reserved.3 *4 * This software is the proprietary copyright of TomTom N.V. and its subsidiaries and may be used5 * for internal evaluation purposes or commercial use strictly subject to separate licensee6 * agreement between you and TomTom. If you are the licensee, you are only permitted to use7 * this Software in accordance with the terms of your license agreement. If you are not the8 * licensee then you are not authorised to use this software in any manner and should9 * immediately return it to TomTom N.V.10 */11package com.tomtom.online.sdk.map;1213import android.graphics.PointF;1415import com.tomtom.online.sdk.annotations.DeprecatedSince;1617import java.io.Serializable;1819/**20 * The anchor property of the marker.21 */22public enum MarkerAnchor implements Serializable {2324 /**25 * Value of(0,-0.5).26 */27 Center(0f, -0.5f),28 /**29 * Value of(-0.5,-0.5).30 */31 Left(-0.5f, -0.5f),3233 /**34 * Value of(0.5,-0.5).35 */36 Right(0.5f, -0.5f),37 /**38 * Value of(0, 0).39 */40 Top(0, 0),4142 /**43 * Value of(0,-1).44 */45 Bottom(0, -1f),4647 /**48 * Value of(-0.5, 0).49 */50 TopLeft(-0.5f, 0),51 /**52 * Value of(0.5,0).53 */54 TopRight(0.5f, 0),5556 /**57 * Value of(-0.5,-1).58 */59 BottomLeft(-0.5f, -1f),6061 /**62 * Value of(0.5,-1).63 */64 BottomRight(0.5f, -1f);6566 /**67 * Defines the icon offset in the x axis.68 *69 * @deprecated Replaced with a getter {@link MarkerAnchor#getIconOffset()}.70 */71 @Deprecated72 @DeprecatedSince(date = "2020.11", replacedWith = "MarkerAnchor.getIconOffset()")73 public float xIconOffset;7475 /**76 * Defines the icon offset in the y axis.77 *78 * @deprecated Replaced with a getter {@link MarkerAnchor#getIconOffset()}.79 */80 @Deprecated81 @DeprecatedSince(date = "2020.11", replacedWith = "MarkerAnchor.getIconOffset()")82 public float yIconOffset;8384 /**85 * Returns the icon offset in the x and y axis for the given {@link MarkerAnchor} value.86 */87 public PointF getIconOffset() {88 return new PointF(xIconOffset, yIconOffset);89 }9091 MarkerAnchor(float xIconOffset, float yIconOffset) {92 this.xIconOffset = xIconOffset;93 this.yIconOffset = yIconOffset;94 }95}
Marker performance
For performance purposes, it is recommended to combine multiple icons into a single drawable, and specify the sub-region of the icon you are interested in. If you use this approach, be sure to leave at least one empty pixel between the icons in your image to keep the edges from blending together as the icons are scaled.