Events

Contents

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:

map.addOnMapClickListener(new TrimbleMapsMap.OnMapClickListener() {
    @Override
    public boolean onMapClick(@NonNull @NotNull LatLng latLng) {
        // latLng being the coordinate of where the user clicked the map
        return true;
    }
});

map.addOnMapLongClickListener(new TrimbleMapsMap.OnMapLongClickListener() {
    @Override
    public boolean onMapLongClick(@NonNull @NotNull LatLng latLng) {
        // latLng being the coordinate where the user long clicked the map
        return true;
    }
});

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(new MapView.OnDidFinishLoadingMapListener() {
    @Override
    public void onDidFinishLoadingMap() {
        // 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(new TrimbleMapsMap.OnCameraMoveStartedListener() {
    @Override
    public void onCameraMoveStarted(int reason) {
        // 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.

map.addOnFlingListener(new TrimbleMapsMap.OnFlingListener() {
    @Override
    public void onFling() {
        // The map was moved by the user with some momentum
    }
});

map.addOnMoveListener(new TrimbleMapsMap.OnMoveListener() {
    @Override
    public void onMoveBegin(@NonNull @NotNull MoveGestureDetector moveGestureDetector) {

    }

    @Override
    public void onMove(@NonNull @NotNull MoveGestureDetector moveGestureDetector) {

    }

    @Override
    public void onMoveEnd(@NonNull @NotNull MoveGestureDetector moveGestureDetector) {

    }
});
Last updated September 22, 2021.