The Maps SDK provides several ways to listen and act upon events fired by the map. The generic listeners are mentioned below. However you may also find specific callbacks exist for specific APIs or uses.
Map click events
Click/Tap events can be listened for within the Map SDK. When set, the callback provided is fired when the user click’s or taps on the map. A LatLng object of where the user clicked is returned as part of the callback. The listeners are added to the TrimbleMapsMap object. Support is available for a regular click as well as a long click:
importcom.trimblemaps.mapsdk.geometry.LatLng;importorg.jetbrains.annotations.NotNull;importandroidx.annotation.NonNull;map.addOnMapClickListener(newTrimbleMapsMap.OnMapClickListener(){@OverridepublicbooleanonMapClick(@NonNull@NotNullLatLnglatLng){// latLng being the coordinate of where the user clicked the mapreturntrue;}});map.addOnMapLongClickListener(newTrimbleMapsMap.OnMapLongClickListener(){@OverridepublicbooleanonMapLongClick(@NonNull@NotNullLatLnglatLng){// latLng being the coordinate where the user long clicked the mapreturntrue;}});
importcom.trimblemaps.mapsdk.geometry.LatLngmap!!.addOnMapClickListener(object: TrimbleMapsMap.OnMapClickListener{overridefunonMapClick(point:LatLng):Boolean{// latLng being the coordinate of where the user clicked the map
returntrue}})map!!.addOnMapLongClickListener(object: TrimbleMapsMap.OnMapLongClickListener{overridefunonMapLongClick(point:LatLng):Boolean{// latLng being the coordinate where the user long clicked the map
returntrue}})
Map change events
While building or changing the map, the MapView object goes through many phases. There are a variety of listeners to use, allowing you to capture when a specific map event has triggered. These listeners are added to the MapView object rather than the TrimbleMapsMap object. For example:
mapView.addOnDidFinishLoadingMapListener(newMapView.OnDidFinishLoadingMapListener(){@OverridepublicvoidonDidFinishLoadingMap(){// The map finished loading}});
mapView!!.addOnDidFinishLoadingMapListener(object: MapView.OnDidFinishLoadingMapListener{overridefunonDidFinishLoadingMap(){// The map finished loading
}})
There are a variety of other events available that are used in a similar manner to the above:
Listener
Description
OnCameraWillChangeListener
Map region is about to change
OnCameraDidChangeListener
Map region completed change
OnWillStartLoadingMapListener
Map about to start loading a new style
OnWillStartRenderingMapListener
Map about to start rendering
addOnWillStartRenderingFrameListener
Map is about to start rendering a frame
OnDidFinishRenderingFrameListener
Map finished rendering the frame
OnDidFinishLoadingStyleListener
Style finished loading
OnSourceChangedListener
A source has changed
OnDidFinishLoadingMapListener
Map finished loading loading a style
OnDidFinishRenderingMapListener
Map has fully rendered
Camera change events
The map’s camera is used to define how the map is viewed; zoom level, rotation, etc. Go here for more details on the Camera itself. There are a few callbacks that you can listen to that relate to some of the camera’s movements, usually related around the beginning and end of a camera movement. See an example below:
map.addOnCameraMoveStartedListener(newTrimbleMapsMap.OnCameraMoveStartedListener(){@OverridepublicvoidonCameraMoveStarted(intreason){// Reasons are static values:// reason == REASON_API_GESTURE - gesture caused the movement// reason == REASON_DEVELOPER_ANIMATION - developer controlled movement// reason == REASON_API_ANIMATION - API animation}});
map!!.addOnCameraMoveStartedListener(object: TrimbleMapsMap.OnCameraMoveStartedListener{overridefunonCameraMoveStarted(reason:Int){// Reasons are static values:
// reason == REASON_API_GESTURE - gesture caused the movement
// reason == REASON_DEVELOPER_ANIMATION - developer controlled movement
// reason == REASON_API_ANIMATION - API animation
}})
These listeners are added to the TrimbleMapsMap object. Other listeners are in the table below:
Listener
Description
OnCameraMove
The camera is moving
OnCameraMoveCancel
The camera’s movement was cancelled
OnCameraIdle
Camera movement has ended
Movement events
Separately from the camera movement events, there are also listeners that can be fired after the user moves or flings the map. If the user uses a single finger to move the screen, this is considered a “Move” event. If the user does the same action but with more momentum, that is considered a “Fling”. These events are registered on a TrimbleMapsMap object.
importcom.trimblemaps.android.gestures.MoveGestureDetector;importorg.jetbrains.annotations.NotNull;importandroidx.annotation.NonNull;map.addOnFlingListener(newTrimbleMapsMap.OnFlingListener(){@OverridepublicvoidonFling(){// The map was moved by the user with some momentum}});map.addOnMoveListener(newTrimbleMapsMap.OnMoveListener(){@OverridepublicvoidonMoveBegin(@NonNull@NotNullMoveGestureDetectormoveGestureDetector){}@OverridepublicvoidonMove(@NonNull@NotNullMoveGestureDetectormoveGestureDetector){}@OverridepublicvoidonMoveEnd(@NonNull@NotNullMoveGestureDetectormoveGestureDetector){}});
importcom.trimblemaps.android.gestures.MoveGestureDetectormap!!.addOnFlingListener(object: TrimbleMapsMap.OnFlingListener{overridefunonFling(){// The map was moved by the user with some momentum
}})map!!.addOnMoveListener(object: TrimbleMapsMap.OnMoveListener{overridefunonMoveBegin(detector:MoveGestureDetector){}overridefunonMove(detector:MoveGestureDetector){}overridefunonMoveEnd(detector:MoveGestureDetector){}})