Request routes outside Drop-In UI 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.
<?xml version="1.0" encoding="utf-8"?><com.mapbox.navigation.dropin.NavigationViewxmlns:app="http://schemas.android.com/apk/res-auto"xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/navigationView"android:layout_width="match_parent"android:layout_height="match_parent"app:accessToken="@string/mapbox_access_token"/>
package com.mapbox.navigation.examples.preview.dropinui import android.location.Locationimport android.os.Bundleimport androidx.appcompat.app.AppCompatActivityimport com.mapbox.api.directions.v5.models.RouteOptionsimport com.mapbox.geojson.Pointimport com.mapbox.maps.MapViewimport com.mapbox.maps.plugin.gestures.OnMapLongClickListenerimport com.mapbox.maps.plugin.gestures.gesturesimport com.mapbox.navigation.base.extensions.applyDefaultNavigationOptionsimport com.mapbox.navigation.base.extensions.applyLanguageAndVoiceUnitOptionsimport com.mapbox.navigation.base.route.NavigationRouteimport com.mapbox.navigation.base.route.NavigationRouterCallbackimport com.mapbox.navigation.base.route.RouterFailureimport com.mapbox.navigation.base.route.RouterOriginimport com.mapbox.navigation.core.lifecycle.MapboxNavigationAppimport com.mapbox.navigation.core.trip.session.LocationMatcherResultimport com.mapbox.navigation.core.trip.session.LocationObserverimport com.mapbox.navigation.dropin.NavigationViewimport com.mapbox.navigation.dropin.map.MapViewObserverimport com.mapbox.navigation.examples.preview.databinding.MapboxActivityRequestRouteNavigationViewBindingimport com.mapbox.navigation.utils.internal.ifNonNull /*** The example demonstrates how to use [MapboxNavigationApp] to request routes outside [NavigationView]* and transition [NavigationView] to active navigation state.** 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>** The example uses replay location engine to facilitate navigation without physically moving.** How to use the example:* - Start the example* - Grant the location permissions if not already granted* - Long press anywhere on the map* - NavigationView should transition to active guidance*/class RequestRouteWithNavigationViewActivity : AppCompatActivity(), OnMapLongClickListener { private var lastLocation: Location? = nullprivate lateinit var binding: MapboxActivityRequestRouteNavigationViewBinding /*** Gets notified with location updates.** Exposes raw updates coming directly from the location services* and the updates enhanced by the Navigation SDK (cleaned up and matched to the road).*/private val locationObserver = object : LocationObserver {override fun onNewLocationMatcherResult(locationMatcherResult: LocationMatcherResult) {lastLocation = locationMatcherResult.enhancedLocation} override fun onNewRawLocation(rawLocation: Location) {// no impl}} /*** Notifies with attach and detach events on [MapView]*/private val mapViewObserver = object : MapViewObserver() {override fun onAttached(mapView: MapView) {mapView.gestures.addOnMapLongClickListener(this@RequestRouteWithNavigationViewActivity)} override fun onDetached(mapView: MapView) {mapView.gestures.removeOnMapLongClickListener(this@RequestRouteWithNavigationViewActivity)}} override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)binding = MapboxActivityRequestRouteNavigationViewBinding.inflate(layoutInflater)setContentView(binding.root) // Set to false if you want to handle map long click listener in the appbinding.navigationView.customizeViewOptions {enableMapLongClickIntercept = false} binding.navigationView.registerMapObserver(mapViewObserver)MapboxNavigationApp.current()?.registerLocationObserver(locationObserver)} override fun onDestroy() {super.onDestroy()binding.navigationView.unregisterMapObserver(mapViewObserver)MapboxNavigationApp.current()?.unregisterLocationObserver(locationObserver)} override fun onMapLongClick(point: Point): Boolean {ifNonNull(lastLocation) {requestRoutes(Point.fromLngLat(it.longitude, it.latitude), point)}return false} private fun requestRoutes(origin: Point, destination: Point) {MapboxNavigationApp.current()!!.requestRoutes(routeOptions = RouteOptions.builder().applyDefaultNavigationOptions().applyLanguageAndVoiceUnitOptions(this).coordinatesList(listOf(origin, destination)).alternatives(true).build(),callback = object : NavigationRouterCallback {override fun onCanceled(routeOptions: RouteOptions, routerOrigin: RouterOrigin) {// no impl} override fun onFailure(reasons: List<RouterFailure>, routeOptions: RouteOptions) {// no impl} override fun onRoutesReady(routes: List<NavigationRoute>,routerOrigin: RouterOrigin) {binding.navigationView.api.routeReplayEnabled(true)binding.navigationView.api.startActiveGuidance(routes)}})}}