-
Notifications
You must be signed in to change notification settings - Fork 52
Example demonstrating how to request routes outside NavigationView
#130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
...com/mapbox/navigation/examples/preview/dropinui/RequestRouteWithNavigationViewActivity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| // 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) | ||
| ) | ||
| } | ||
| } | ||
| ) | ||
| } | ||
| } | ||
Binary file added
BIN
+34.2 KB
...ion/examples/preview/dropinui/res/drawable/mapbox_screenshot_request_routes.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions
9
...on/examples/preview/dropinui/res/layout/mapbox_activity_request_route_navigation_view.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.