title | description | author | ms.author | ms.date | ms.topic | ms.service | services | zone_pivot_groups |
---|---|---|---|---|---|---|---|---|
Handle map events in Android maps | Microsoft Azure Maps |
Learn which events are fired when users interact with maps. View a list of all supported map events. See how to use the Azure Maps Android SDK to handle events. |
stevemunk |
v-munksteve |
2/26/2021 |
conceptual |
azure-maps |
azure-maps |
azure-maps-android |
This article shows you how to use the maps events manager.
The map manages all events through its events
property. The following table lists all of the supported map events.
Event | Event handler format | Description |
---|---|---|
OnCameraIdle |
() |
Fired after the last frame rendered before the map enters an "idle" state:
|
OnCameraMove |
() |
Fired repeatedly during an animated transition from one view to another, as the result of either user interaction or methods. |
OnCameraMoveCanceled |
() |
Fired when a movement request to the camera has been canceled. |
OnCameraMoveStarted |
(int reason) |
Fired just before the map begins a transition from one view to another, as the result of either user interaction or methods. The reason argument of the event listener returns an integer value that provides details of how the camera movement was initiated. The following list outlines the possible reasons:
|
OnClick |
(double lat, double lon): boolean |
Fired when the map is pressed and released at the same point on the map. This event handler returns a boolean value indicating if the event should be consumed or passed further to other event listeners. |
OnFeatureClick |
(List<Feature>): boolean |
Fired when the map is pressed and released at the same point on a feature. This event handler returns a boolean value indicating if the event should be consumed or passed further to other event listeners. |
OnLayerAdded |
(Layer layer) |
Fired when a layer is added to the map. |
OnLayerRemoved |
(Layer layer) |
Fired when a layer is removed from the map. |
OnLoaded |
() |
Fired immediately after all necessary resources have been downloaded and the first visually complete rendering of the map has occurred. |
OnLongClick |
(double lat, double lon): boolean |
Fired when the map is pressed, held for a moment, and then released at the same point on the map. This event handler returns a boolean value indicating if the event should be consumed or passed further to other event listeners. |
OnLongFeatureClick |
(List<Feature>): boolean |
Fired when the map is pressed, held for a moment, and then released at the same point on a feature. This event handler returns a boolean value indicating if the event should be consumed or passed further to other event listeners. |
OnReady |
(AzureMap map) |
Fired when the map initially is loaded or when the app orientation change and the minimum required map resources are loaded and the map is ready to be programmatically interacted with. |
OnSourceAdded |
(Source source) |
Fired when a DataSource or VectorTileSource is added to the map. |
OnSourceRemoved |
(Source source) |
Fired when a DataSource or VectorTileSource is removed from the map. |
OnStyleChange |
() |
Fired when the map's style loads or changes. |
The following code shows how to add the OnClick
, OnFeatureClick
, and OnCameraMove
events to the map.
::: zone pivot="programming-language-java-android"
map.events.add((OnClick) (lat, lon) -> {
//Map clicked.
//Return true indicating if event should be consumed and not passed further to other listeners registered afterwards, false otherwise.
return true;
});
map.events.add((OnFeatureClick) (features) -> {
//Feature clicked.
//Return true indicating if event should be consumed and not passed further to other listeners registered afterwards, false otherwise.
return true;
});
map.events.add((OnCameraMove) () -> {
//Map camera moved.
});
::: zone-end
::: zone pivot="programming-language-kotlin"
map.events.add(OnClick { lat: Double, lon: Double ->
//Map clicked.
//Return true indicating if event should be consumed and not passed further to other listeners registered afterwards, false otherwise.
return false
})
map.events.add(OnFeatureClick { features: List<Feature?>? ->
//Feature clicked.
//Return true indicating if event should be consumed and not passed further to other listeners registered afterwards, false otherwise.
return false
})
map.events.add(OnCameraMove {
//Map camera moved.
})
::: zone-end
For more information, see the Navigating the map documentation on how to interact with the map and trigger events.
When adding the OnFeatureClick
or OnLongFeatureClick
events to the map, a layer instance or layer ID can be passed in as a second parameter. When a layer is passed in, the event will only fire if the event occurs on that layer. Events scoped to layers are supported by the symbol, bubble, line, and polygon layers.
::: zone pivot="programming-language-java-android"
//Create a data source.
DataSource source = new DataSource();
map.sources.add(source);
//Add data to the data source.
source.add(Point.fromLngLat(0, 0));
//Create a layer and add it to the map.
BubbleLayer layer = new BubbleLayer(source);
map.layers.add(layer);
//Add a feature click event to the map and pass the layer ID to limit the event to the specified layer.
map.events.add((OnFeatureClick) (features) -> {
//One or more features clicked.
//Return true indicating if event should be consumed and not passed further to other listeners registered afterwards, false otherwise.
return true;
}, layer);
//Add a long feature click event to the map and pass the layer ID to limit the event to the specified layer.
map.events.add((OnLongFeatureClick) (features) -> {
//One or more features long clicked.
//Return true indicating if event should be consumed and not passed further to other listeners registered afterwards, false otherwise.
return true;
}, layer);
::: zone-end
::: zone pivot="programming-language-kotlin"
//Create a data source.
val source = DataSource()
map.sources.add(source)
//Add data to the data source.
source.add(Point.fromLngLat(0, 0))
//Create a layer and add it to the map.
val layer = BubbleLayer(source)
map.layers.add(layer)
//Add a feature click event to the map and pass the layer ID to limit the event to the specified layer.
map.events.add(
OnFeatureClick { features: List<Feature?>? ->
//One or more features clicked.
//Return true indicating if event should be consumed and not passed further to other listeners registered afterwards, false otherwise.
return false
},
layer
)
//Add a long feature click event to the map and pass the layer ID to limit the event to the specified layer.
map.events.add(
OnLongFeatureClick { features: List<Feature?>? ->
//One or more features long clicked.
//Return true indicating if event should be consumed and not passed further to other listeners registered afterwards, false otherwise.
return false
},
layer
)
::: zone-end
See the following articles for full code examples:
[!div class="nextstepaction"] Navigating the map
[!div class="nextstepaction"] Add a symbol layer
[!div class="nextstepaction"] Add a bubble layer
[!div class="nextstepaction"] Add a line layer
[!div class="nextstepaction"] Add a polygon layer