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
InterceptMapClickActivity view source
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.