• Optimization
  • Navigation
  • Tracking
  • Maps
  • Places

Intercept map click and long click events using NavNextbillionMap

This example shows how to intercept the NavNextbillionMap click and long click events and add corresponding actions to them. Currently, a toast message is added to each click and long click event, which shows the tapped location latitude and longitude.

For all code examples, refer to Navigation Code Examples

activity_intercept_click_events.xml view source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.InterceptMapClickActivity">

    <ai.nextbillion.maps.core.MapView
        android:id="@+id/mapView_intercept_click_events"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:nbmap_uiAttribution="false" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

InterceptMapClickActivity view source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package ai.nextbillion.navigation.demo.activity;

import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import ai.nextbillion.kits.geojson.Point;
import ai.nextbillion.maps.camera.CameraUpdate;
import ai.nextbillion.maps.camera.CameraUpdateFactory;
import ai.nextbillion.maps.core.MapView;
import ai.nextbillion.maps.core.NextbillionMap;
import ai.nextbillion.maps.core.OnMapReadyCallback;
import ai.nextbillion.maps.core.Style;
import ai.nextbillion.maps.geometry.LatLng;
import ai.nextbillion.maps.location.modes.RenderMode;
import ai.nextbillion.navigation.demo.R;
import ai.nextbillion.navigation.ui.camera.CameraUpdateMode;
import ai.nextbillion.navigation.ui.camera.NavigationCameraUpdate;
import ai.nextbillion.navigation.ui.map.NavNextbillionMap;

/***
 * This class shows how to intercept the click and long click events of nbMap
 */

public class InterceptMapClickActivity extends AppCompatActivity implements OnMapReadyCallback, NextbillionMap.OnMapClickListener,
        NextbillionMap.OnMapLongClickListener {
    private static final int CAMERA_ANIMATION_DURATION = 1000;
    private static final int DEFAULT_CAMERA_ZOOM = 16;

    private MapView mapView;
    private NavNextbillionMap nbMap;

    // a fake point in Singapore
    private final Point fakePoint = Point.fromLngLat(103.852408, 1.276411);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intercept_click_events);
        mapView = findViewById(R.id.mapView_intercept_click_events);
        mapView.onCreate(savedInstanceState);
        mapView.getMapAsync(this);
    }

    @Override
    public boolean onMapClick(@NonNull LatLng latLng) {
        Toast.makeText(
                InterceptMapClickActivity.this,
                String.format("Map was clicked with lat:%s, long: %s", latLng.getLatitude(), latLng.getLongitude()),
                Toast.LENGTH_LONG).show();
        return false;
    }

    @Override
    public boolean onMapLongClick(@NonNull LatLng latLng) {
        Toast.makeText(
                InterceptMapClickActivity.this,
                String.format("Map was long clicked with lat:%s, long: %s", latLng.getLatitude(), latLng.getLongitude()),
                Toast.LENGTH_LONG).show();
        return false;
    }

    @Override
    public void onMapReady(@NonNull NextbillionMap nextbillionMap) {
        nextbillionMap.setStyle(new Style.Builder().fromUri("https://api.nextbillion.io/maps/streets/style.json"));
        nextbillionMap.getStyle(style -> {
            // add the following listeners to nextbillionMap so the override onClick methods could work
            nextbillionMap.addOnMapClickListener(InterceptMapClickActivity.this);
            nextbillionMap.addOnMapLongClickListener(InterceptMapClickActivity.this);
            nbMap = new NavNextbillionMap(mapView, nextbillionMap);
            nbMap.updateLocationLayerRenderMode(RenderMode.COMPASS);
            animateCamera();
        });
    }

    private void animateCamera() {
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(fakePoint.latitude(), fakePoint.longitude()), DEFAULT_CAMERA_ZOOM);
        NavigationCameraUpdate navigationCameraUpdate = new NavigationCameraUpdate(cameraUpdate);
        navigationCameraUpdate.setMode(CameraUpdateMode.OVERRIDE);
        nbMap.retrieveCamera().update(navigationCameraUpdate, CAMERA_ANIMATION_DURATION);
    }

    @Override
    public void onStart() {
        super.onStart();
        mapView.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
        mapView.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }
}

Code Highlights

The InterceptMapClickActivity class can be used to intercept clicks and long clicks on a map. It can be used to display a Toast message with the latitude and longitude of the clicked point.

The single tap event is intercepted in onMapClick() method, and the long click event is intercepted in onMapLongClick() method.

Code summary

The InterceptMapClickActivity class extends AppCompatActivity and implements the OnMapReadyCallback, NextbillionMap.OnMapClickListener, and NextbillionMap.OnMapLongClickListener interfaces. It uses the NextbillionMap library to display a map and listen for clicks and long clicks on the map.

The class has the following members:

  • CAMERA_ANIMATION_DURATION: The duration of the camera animation when the map is initialized.

  • DEFAULT_CAMERA_ZOOM: The default zoom level of the map.

  • mapView: A MapView object that displays the map.

  • nbMap: A NavNextbillionMap object that provides access to the NextbillionMap library.

  • fakePoint: A Point object that represents a fake point in Singapore.

The class implements the following methods:

  • onCreate(): This method is called when the activity is created. It initializes the mapView object and sets the CAMERA_ANIMATION_DURATION and DEFAULT_CAMERA_ZOOM constants. It also gets the map style from the NextbillionMap library and adds the OnMapClickListener and OnMapLongClickListener interfaces to the map.

  • onMapClick(): This method is called when the map is clicked. It displays a Toast message with the latitude and longitude of the clicked point.

  • onMapLongClick(): This method is called when the map is long-clicked. It displays a Toast message with the latitude and longitude of the clicked point.

  • onMapReady(): This method is called when the map is ready. It sets the style of the map and animates the camera to the fakePoint location.

  • animateCamera(): This method animates the camera to the fakePoint location.

  • onStart(): This method is called when the activity starts. It calls the onStart() method of the mapView object.

  • onResume(): This method is called when the activity resumes. It calls the onResume() method of the mapView object.

  • onLowMemory(): This method is called when the device is low on memory. It calls the onLowMemory() method of the mapView object.

  • onPause(): This method is called when the activity pauses. It calls the onPause() method of the mapView object.

  • onStop(): This method is called when the activity stops. It calls the onStop() method of the mapView object.

  • onDestroy(): This method is called when the activity is destroyed. It calls the onDestroy() method of the mapView object.