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<?xml version="1.0" encoding="utf-8"?>
2<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
3    xmlns:app="http://schemas.android.com/apk/res-auto"
4    xmlns:tools="http://schemas.android.com/tools"
5    android:layout_width="match_parent"
6    android:layout_height="match_parent"
7    tools:context=".activity.InterceptMapClickActivity">
8
9    <ai.nextbillion.maps.core.MapView
10        android:id="@+id/mapView_intercept_click_events"
11        android:layout_width="match_parent"
12        android:layout_height="match_parent"
13        app:nbmap_uiAttribution="false" />
14
15</androidx.coordinatorlayout.widget.CoordinatorLayout>

InterceptMapClickActivity view source

1package ai.nextbillion.navigation.demo.activity;
2
3import android.os.Bundle;
4import android.widget.Toast;
5
6import androidx.annotation.NonNull;
7import androidx.annotation.Nullable;
8import androidx.appcompat.app.AppCompatActivity;
9
10import ai.nextbillion.kits.geojson.Point;
11import ai.nextbillion.maps.camera.CameraUpdate;
12import ai.nextbillion.maps.camera.CameraUpdateFactory;
13import ai.nextbillion.maps.core.MapView;
14import ai.nextbillion.maps.core.NextbillionMap;
15import ai.nextbillion.maps.core.OnMapReadyCallback;
16import ai.nextbillion.maps.core.Style;
17import ai.nextbillion.maps.geometry.LatLng;
18import ai.nextbillion.maps.location.modes.RenderMode;
19import ai.nextbillion.navigation.demo.R;
20import ai.nextbillion.navigation.ui.camera.CameraUpdateMode;
21import ai.nextbillion.navigation.ui.camera.NavigationCameraUpdate;
22import ai.nextbillion.navigation.ui.map.NavNextbillionMap;
23
24/***
25 * This class shows how to intercept the click and long click events of nbMap
26 */
27
28public class InterceptMapClickActivity extends AppCompatActivity implements OnMapReadyCallback, NextbillionMap.OnMapClickListener,
29        NextbillionMap.OnMapLongClickListener {
30    private static final int CAMERA_ANIMATION_DURATION = 1000;
31    private static final int DEFAULT_CAMERA_ZOOM = 16;
32
33    private MapView mapView;
34    private NavNextbillionMap nbMap;
35
36    // a fake point in Singapore
37    private final Point fakePoint = Point.fromLngLat(103.852408, 1.276411);
38
39    @Override
40    protected void onCreate(@Nullable Bundle savedInstanceState) {
41        super.onCreate(savedInstanceState);
42        setContentView(R.layout.activity_intercept_click_events);
43        mapView = findViewById(R.id.mapView_intercept_click_events);
44        mapView.onCreate(savedInstanceState);
45        mapView.getMapAsync(this);
46    }
47
48    @Override
49    public boolean onMapClick(@NonNull LatLng latLng) {
50        Toast.makeText(
51                InterceptMapClickActivity.this,
52                String.format("Map was clicked with lat:%s, long: %s", latLng.getLatitude(), latLng.getLongitude()),
53                Toast.LENGTH_LONG).show();
54        return false;
55    }
56
57    @Override
58    public boolean onMapLongClick(@NonNull LatLng latLng) {
59        Toast.makeText(
60                InterceptMapClickActivity.this,
61                String.format("Map was long clicked with lat:%s, long: %s", latLng.getLatitude(), latLng.getLongitude()),
62                Toast.LENGTH_LONG).show();
63        return false;
64    }
65
66    @Override
67    public void onMapReady(@NonNull NextbillionMap nextbillionMap) {
68        nextbillionMap.setStyle(new Style.Builder().fromUri("https://api.nextbillion.io/maps/streets/style.json"));
69        nextbillionMap.getStyle(style -> {
70            // add the following listeners to nextbillionMap so the override onClick methods could work
71            nextbillionMap.addOnMapClickListener(InterceptMapClickActivity.this);
72            nextbillionMap.addOnMapLongClickListener(InterceptMapClickActivity.this);
73            nbMap = new NavNextbillionMap(mapView, nextbillionMap);
74            nbMap.updateLocationLayerRenderMode(RenderMode.COMPASS);
75            animateCamera();
76        });
77    }
78
79    private void animateCamera() {
80        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(fakePoint.latitude(), fakePoint.longitude()), DEFAULT_CAMERA_ZOOM);
81        NavigationCameraUpdate navigationCameraUpdate = new NavigationCameraUpdate(cameraUpdate);
82        navigationCameraUpdate.setMode(CameraUpdateMode.OVERRIDE);
83        nbMap.retrieveCamera().update(navigationCameraUpdate, CAMERA_ANIMATION_DURATION);
84    }
85
86    @Override
87    public void onStart() {
88        super.onStart();
89        mapView.onStart();
90    }
91
92    @Override
93    public void onResume() {
94        super.onResume();
95        mapView.onResume();
96    }
97
98    @Override
99    public void onLowMemory() {
100        super.onLowMemory();
101        mapView.onLowMemory();
102    }
103
104    @Override
105    public void onPause() {
106        super.onPause();
107        mapView.onPause();
108    }
109
110    @Override
111    public void onStop() {
112        super.onStop();
113        mapView.onStop();
114    }
115
116    @Override
117    protected void onDestroy() {
118        super.onDestroy();
119        mapView.onDestroy();
120    }
121}

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:

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

  2. DEFAULT_CAMERA_ZOOM: The default zoom level of the map.

  3. mapView: A MapView object that displays the map.

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

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

The class implements the following methods:

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

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

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

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

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

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

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

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

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

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

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

没找到你要找的内容?