Intercept map long click events using Navigation View
Note
This example is a part of the Navigation SDK Examples. You can find the values for all referenced resources in the res
directory. For example, see res/values/strings.xml
for R.string.*
references used in this example. The dependencies can be found here.The examples use View binding.See setup documention if necessary.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.mapbox.navigation.dropin.NavigationView
android:id="@+id/navigationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:accessToken="@string/mapbox_access_token" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.mapbox.navigation.examples.dropinui.viewinjection
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.mapbox.geojson.Point
import com.mapbox.maps.MapView
import com.mapbox.maps.plugin.gestures.OnMapLongClickListener
import com.mapbox.maps.plugin.gestures.gestures
import com.mapbox.navigation.dropin.map.MapViewObserver
import com.mapbox.navigation.examples.databinding.MapboxActivityCustomLongClickBinding
/**
* The example demonstrates how to add your own long click listener on the `MapView` and disable
* `NavigationView` from handling it.
*
* Before running the example make sure you have put your access_token in the correct place
* inside [app-preview/src/main/res/values/mapbox_access_token.xml]. If not present then add
* this file at the location mentioned above and add the following content to it
*
* <?xml version="1.0" encoding="utf-8"?>
* <resources xmlns:tools="http://schemas.android.com/tools">
* <string name="mapbox_access_token"><PUT_YOUR_ACCESS_TOKEN_HERE></string>
* </resources>
*
* How to use the example:
* - Start the example
* - Long click on the `MapView`
*/
class CustomLongClickActivity : AppCompatActivity() {
private lateinit var binding: MapboxActivityCustomLongClickBinding
private val onMapLongClick = object : MapViewObserver(), OnMapLongClickListener {
override fun onAttached(mapView: MapView) {
mapView.gestures.addOnMapLongClickListener(this)
}
override fun onDetached(mapView: MapView) {
mapView.gestures.removeOnMapLongClickListener(this)
}
override fun onMapLongClick(point: Point): Boolean {
Toast.makeText(
this@CustomLongClickActivity,
"Map was long clicked",
Toast.LENGTH_LONG
).show()
return false
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = MapboxActivityCustomLongClickBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.navigationView.registerMapObserver(onMapLongClick)
binding.navigationView.customizeViewOptions {
enableMapLongClickIntercept = false
}
}
override fun onDestroy() {
super.onDestroy()
binding.navigationView.unregisterMapObserver(onMapLongClick)
}
}
この{Type}は役に立ちましたか?