# 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.

![documentation image](<https://static.nextbillion.io/docs-next/docs/navigation/android/ click-and-long-click-events-using navnextbillionmap.webp>)

For all code examples, refer to [Navigation Code Examples](https://github.com/nextbillion-ai/nb-navigation-android-demo)

**activity_intercept_click_events.xml [view source](https://github.com/nextbillion-ai/nb-navigation-android-demo/blob/main/app/src/main/res/layout/activity_intercept_click_events.xml)**

```xml
<?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](https://github.com/nextbillion-ai/nb-navigation-android-demo/blob/main/app/src/main/java/ai/nextbillion/navigation/demo/activity/InterceptMapClickActivity.java)**

```java
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.
