Fetch routes between an origin and destination
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"?><androidx.constraintlayout.widget.ConstraintLayout 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"> <ScrollViewandroid:id="@+id/responseContainer"android:layout_width="match_parent"android:layout_height="0dp"app:layout_constraintBottom_toTopOf="@id/fetchARouteButton"app:layout_constraintTop_toTopOf="parent"> <TextViewandroid:id="@+id/responseTextView"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="16dp"android:text="ready for request"tools:ignore="HardcodedText" /> </ScrollView> <androidx.appcompat.widget.AppCompatButtonandroid:id="@+id/fetchARouteButton"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginStart="8dp"android:layout_marginEnd="8dp"android:layout_marginBottom="8dp"android:background="@drawable/mapbox_button"android:padding="12dp"android:textAllCaps="false"android:textColor="@android:color/white"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>
package com.mapbox.navigation.examples.fetchroute import android.annotation.SuppressLintimport android.location.Locationimport android.os.Bundleimport android.util.Logimport android.view.View.GONEimport android.view.View.VISIBLEimport androidx.appcompat.app.AppCompatActivityimport com.google.gson.GsonBuilderimport com.google.gson.JsonParserimport com.mapbox.api.directions.v5.models.Bearingimport com.mapbox.api.directions.v5.models.RouteOptionsimport com.mapbox.geojson.Pointimport com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPIimport com.mapbox.navigation.base.extensions.applyDefaultNavigationOptionsimport com.mapbox.navigation.base.extensions.applyLanguageAndVoiceUnitOptionsimport com.mapbox.navigation.base.options.NavigationOptionsimport 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.MapboxNavigationimport com.mapbox.navigation.core.lifecycle.MapboxNavigationAppimport com.mapbox.navigation.core.lifecycle.requireMapboxNavigationimport com.mapbox.navigation.examples.Rimport com.mapbox.navigation.examples.databinding.MapboxActivityFetchARouteBindingimport com.mapbox.navigation.examples.location.ShowCurrentLocationActivity /*** The example demonstrates given a pair of coordinates how to fetch a route.** Before running the example make sure you do the following:* - Put your access_token in the correct place inside [app/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">YOUR_ACCESS_TOKEN_HERE</string>* </resources>* - Add MAPBOX_DOWNLOADS_TOKEN to your USER_HOME»/.gradle/gradle.properties file.* To find out how to get your MAPBOX_DOWNLOADS_TOKEN follow these steps.* https://docs.mapbox.com/android/beta/navigation/guides/install/#configure-credentials** For the purposes of this example the code will not hook onto your current* location. Origin and destination coordinates will be hardcoded. To understand how to* listen to your own location updates go through this example [ShowCurrentLocationActivity]** How to use this example:* - Click on the example with title (Fetch routes between origin and destination) from the list of examples.* - You should see a map view and a button.* - Tap on the button that says Fetch A Route.* - The button should disappear and you should see a result in a text view** Note: The aim of this example is to only show how to request a route. Once the route is* requested, neither it is drawn nor any after affects are reflected on the map.*/@OptIn(ExperimentalPreviewMapboxNavigationAPI::class)class FetchARouteActivity : AppCompatActivity() { private companion object {private const val LOG_TAG = "FetchARouteActivity"} /*** Bindings to the example layout.*/private val binding: MapboxActivityFetchARouteBinding by lazy {MapboxActivityFetchARouteBinding.inflate(layoutInflater)} private val originLocation = Location("test").apply {longitude = -122.4192latitude = 37.7627bearing = 10f}private val destination = Point.fromLngLat(-122.4106, 37.7676) /*** Mapbox Navigation entry point. There should only be one instance of this object for the app.*/private val mapboxNavigation: MapboxNavigation by requireMapboxNavigation(onInitialize = this::initNavigation) @SuppressLint("SetTextI18n")override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(binding.root) binding.fetchARouteButton.text = "Fetch A Route"binding.fetchARouteButton.setOnClickListener { fetchARoute() }} private fun initNavigation() {MapboxNavigationApp.setup(NavigationOptions.Builder(this).accessToken(getString(R.string.mapbox_access_token)).build())} /*** The method instantiates a [RouteOptions] object and fetches route between the origin and* destination pair. There are several [RouteOptions] that you can specify, but this example* mentions only what is relevant.*/@SuppressLint("SetTextI18n")private fun fetchARoute() {binding.responseTextView.text = "fetching route..." val originPoint = Point.fromLngLat(originLocation.longitude,originLocation.latitude) val routeOptions = RouteOptions.builder()// applies the default parameters to route options.applyDefaultNavigationOptions().applyLanguageAndVoiceUnitOptions(this)// lists the coordinate pair i.e. origin and destination// If you want to specify waypoints you can pass list of points instead of null.coordinatesList(listOf(originPoint, destination))// set it to true if you want to receive alternate routes to your destination.alternatives(false)// provide the bearing for the origin of the request to ensure// that the returned route faces in the direction of the current user movement.bearingsList(listOf(Bearing.builder().angle(originLocation.bearing.toDouble()).degrees(45.0).build(),null)).build()mapboxNavigation.requestRoutes(routeOptions,object : NavigationRouterCallback {override fun onCanceled(routeOptions: RouteOptions, routerOrigin: RouterOrigin) {// This particular callback is executed if you invoke// mapboxNavigation.cancelRouteRequest()binding.responseTextView.text = "route request canceled"binding.fetchARouteButton.visibility = VISIBLE} override fun onFailure(reasons: List<RouterFailure>, routeOptions: RouteOptions) {binding.responseTextView.text ="""route request failed with:$reasons""".trimIndent()Log.e(LOG_TAG, "route request failed with $reasons")binding.fetchARouteButton.visibility = VISIBLE} override fun onRoutesReady(routes: List<NavigationRoute>,routerOrigin: RouterOrigin) {// GSON instance used only to print the response prettilyval gson = GsonBuilder().setPrettyPrinting().create()val json = routes.map {gson.toJson(JsonParser.parseString(it.directionsRoute.toJson()))}binding.responseTextView.text ="""|routes ready (origin: ${routerOrigin::class.simpleName}):|$json""".trimMargin()}})binding.fetchARouteButton.visibility = GONE}}