Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app-preview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ android {

dependencies {
// Mapbox Navigation SDK
implementation "com.mapbox.navigation:ui-app:2.7.0-rc.1"
implementation "com.mapbox.navigation:ui-dropin:2.7.0-rc.1"
implementation "com.mapbox.navigation:ui-app:2.8.0-alpha.3"
implementation "com.mapbox.navigation:ui-dropin:2.8.0-alpha.3"
implementation "com.mapbox.mapboxsdk:mapbox-android-core:5.0.1"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.31"
implementation "androidx.core:core-ktx:1.7.0"
Expand Down
4 changes: 4 additions & 0 deletions app-preview/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,9 @@
<activity
android:name=".dropinui.CustomInfoPanelAttributesActivity"
android:exported="true" />

<activity
android:name=".dropinui.RequestRouteWithNavigationViewActivity"
android:exported="true" />
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.mapbox.navigation.examples.preview.dropinui.CustomSpeedLimitActivity
import com.mapbox.navigation.examples.preview.dropinui.CustomTripProgressActivity
import com.mapbox.navigation.examples.preview.dropinui.CustomViewInjectionActivity
import com.mapbox.navigation.examples.preview.dropinui.NavigationViewActivity
import com.mapbox.navigation.examples.preview.dropinui.RequestRouteWithNavigationViewActivity
import com.mapbox.navigation.examples.preview.dropinui.ToggleThemeActivity

fun Context.examplesList() = listOf(
Expand Down Expand Up @@ -73,4 +74,13 @@ fun Context.examplesList() = listOf(
getString(R.string.description_customize_info_panel_attributes),
CustomInfoPanelAttributesActivity::class.java
),
MapboxExample(
ContextCompat.getDrawable(
this,
R.drawable.mapbox_screenshot_request_routes
),
getString(R.string.title_request_route_outside_navigation_view),
getString(R.string.description_request_route_outside_navigation_view),
RequestRouteWithNavigationViewActivity::class.java
),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package com.mapbox.navigation.examples.preview.dropinui

import android.location.Location
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.mapbox.api.directions.v5.models.RouteOptions
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.base.ExperimentalPreviewMapboxNavigationAPI
import com.mapbox.navigation.base.extensions.applyDefaultNavigationOptions
import com.mapbox.navigation.base.extensions.applyLanguageAndVoiceUnitOptions
import com.mapbox.navigation.base.route.NavigationRoute
import com.mapbox.navigation.base.route.NavigationRouterCallback
import com.mapbox.navigation.base.route.RouterFailure
import com.mapbox.navigation.base.route.RouterOrigin
import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp
import com.mapbox.navigation.core.trip.session.LocationMatcherResult
import com.mapbox.navigation.core.trip.session.LocationObserver
import com.mapbox.navigation.dropin.MapViewObserver
import com.mapbox.navigation.examples.preview.databinding.MapboxActivityRequestRouteNavigationViewBinding
import com.mapbox.navigation.ui.app.internal.SharedApp
import com.mapbox.navigation.ui.app.internal.navigation.NavigationState
import com.mapbox.navigation.ui.app.internal.navigation.NavigationStateAction
import 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
*/
@OptIn(ExperimentalPreviewMapboxNavigationAPI::class)
class RequestRouteWithNavigationViewActivity : AppCompatActivity(), OnMapLongClickListener {

private var lastLocation: Location? = null
private 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 app
binding.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.enableReplaySession()
// Sets the destination.
binding.navigationView.api.setDestination(destination)
// Sets routes to preview state.
binding.navigationView.api.setPreviewRoutes(routes)
Comment on lines +140 to +141

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need this if we go to the AG state immediately?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You go into route preview state by pressing the back button. Without this call there won't be any routes in this state.

// Sets routes to mapbox navigation.
binding.navigationView.api.setRoutes(routes)
// This is discouraged and temporary. Public API(s) will be exposed in upcoming
// versions that would allow you to transition NavigationView to ActiveGuidance
// state
SharedApp.store.dispatch(
NavigationStateAction.Update(NavigationState.ActiveNavigation)
)
}
}
)
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<com.mapbox.navigation.dropin.NavigationView
xmlns: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"
/>
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@
<string name="title_customize_info_panel_attributes">Customize info panel attributes using NavigationView</string>
<string name="description_customize_info_panel_attributes">The example demonstrates how to change margins, background and peek height of the default info panel.</string>

<string name="title_request_route_outside_navigation_view">Request routes using MapboxNavigation</string>
<string name="description_request_route_outside_navigation_view">The example demonstrates how to request routes using mapbox navigation and transition NavigationView to active guidance state</string>

</resources>