feat: Navigation observer
This commit is contained in:
parent
9db0e3611c
commit
0a1c908a9d
4 changed files with 90 additions and 8 deletions
|
@ -32,7 +32,8 @@ class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
private lateinit var binding: ActivityMainBinding
|
private lateinit var binding: ActivityMainBinding
|
||||||
private lateinit var mapControl: MapControl
|
private lateinit var mapControl: MapControl
|
||||||
private val navigationObserver = NavigationObserver()
|
private lateinit var navigationObserver: NavigationObserver
|
||||||
|
private lateinit var initThread: Thread
|
||||||
val permissionsHelper = PermissionsHelper(WeakReference(this))
|
val permissionsHelper = PermissionsHelper(WeakReference(this))
|
||||||
|
|
||||||
init {
|
init {
|
||||||
|
@ -62,13 +63,27 @@ class MainActivity : AppCompatActivity() {
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
MapboxNavigationApp.current()?.startTripSession()
|
mapControl = MapControl(binding.mapView, resources)
|
||||||
mapControl = MapControl(this, binding.mapView, resources)
|
|
||||||
mapControl.init()
|
mapControl.init()
|
||||||
|
navigationObserver = NavigationObserver(mapControl)
|
||||||
}
|
}
|
||||||
|
initThread = Thread {
|
||||||
|
while (true) {
|
||||||
|
if (MapboxNavigationApp.current() != null) {
|
||||||
|
MapboxNavigationApp.current()!!.startTripSession()
|
||||||
|
threadCallback()
|
||||||
|
}
|
||||||
|
Thread.sleep(100)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
initThread.start()
|
||||||
bluetoothInit()
|
bluetoothInit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun threadCallback() {
|
||||||
|
initThread.join()
|
||||||
|
}
|
||||||
|
|
||||||
override fun onStart() {
|
override fun onStart() {
|
||||||
super.onStart()
|
super.onStart()
|
||||||
// MapboxNavigationApp.current()?.registerRouteProgressObserver(routeProgressObserver)
|
// MapboxNavigationApp.current()?.registerRouteProgressObserver(routeProgressObserver)
|
||||||
|
|
|
@ -2,6 +2,7 @@ package eu.ztsh.garmin.mapbox
|
||||||
|
|
||||||
import android.content.res.Resources
|
import android.content.res.Resources
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import com.mapbox.api.directions.v5.models.RouteOptions
|
||||||
import com.mapbox.maps.EdgeInsets
|
import com.mapbox.maps.EdgeInsets
|
||||||
import com.mapbox.maps.MapView
|
import com.mapbox.maps.MapView
|
||||||
import com.mapbox.maps.Style
|
import com.mapbox.maps.Style
|
||||||
|
@ -10,12 +11,45 @@ import com.mapbox.maps.plugin.locationcomponent.location
|
||||||
import com.mapbox.maps.plugin.viewport.data.FollowPuckViewportStateOptions
|
import com.mapbox.maps.plugin.viewport.data.FollowPuckViewportStateOptions
|
||||||
import com.mapbox.maps.plugin.viewport.state.FollowPuckViewportState
|
import com.mapbox.maps.plugin.viewport.state.FollowPuckViewportState
|
||||||
import com.mapbox.maps.plugin.viewport.viewport
|
import com.mapbox.maps.plugin.viewport.viewport
|
||||||
import eu.ztsh.garmin.MainActivity
|
import com.mapbox.navigation.base.extensions.applyDefaultNavigationOptions
|
||||||
|
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.ui.maps.location.NavigationLocationProvider
|
||||||
|
import com.mapbox.navigation.utils.internal.toPoint
|
||||||
|
|
||||||
|
class MapControl(val mapView: MapView, private val resources: Resources) {
|
||||||
|
|
||||||
|
val navigationLocationProvider = NavigationLocationProvider()
|
||||||
|
|
||||||
|
val routesRequestCallback = object : NavigationRouterCallback {
|
||||||
|
override fun onRoutesReady(routes: List<NavigationRoute>, @RouterOrigin routerOrigin: String) {
|
||||||
|
MapboxNavigationApp.current()?.setNavigationRoutes(routes)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onFailure(reasons: List<RouterFailure>, routeOptions: RouteOptions) {
|
||||||
|
Log.e(TAG, "onFailure: ")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCanceled(routeOptions: RouteOptions, @RouterOrigin routerOrigin: String) {
|
||||||
|
Log.w(TAG, "onCanceled: ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class MapControl(private val context: MainActivity, private val mapView: MapView, private val resources: Resources) {
|
|
||||||
|
|
||||||
fun init() {
|
fun init() {
|
||||||
mapView.mapboxMap.loadStyle(Style.TRAFFIC_DAY) // TODO: base on sun position
|
mapView.mapboxMap.loadStyle(Style.TRAFFIC_DAY) // TODO: base on sun position
|
||||||
|
|
||||||
|
mapView.location.apply {
|
||||||
|
// locationProvider = this.getLocationProvider()
|
||||||
|
// setLocationProvider(navigationLocationProvider)
|
||||||
|
puckBearingEnabled = true
|
||||||
|
enabled = true
|
||||||
|
}
|
||||||
|
|
||||||
follow(true)
|
follow(true)
|
||||||
setGestures(mapView)
|
setGestures(mapView)
|
||||||
}
|
}
|
||||||
|
@ -52,6 +86,16 @@ class MapControl(private val context: MainActivity, private val mapView: MapView
|
||||||
}
|
}
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
addOnMapLongClickListener { point ->
|
||||||
|
MapboxNavigationApp.current()?.requestRoutes(
|
||||||
|
RouteOptions.builder()
|
||||||
|
.applyDefaultNavigationOptions()
|
||||||
|
.coordinatesList(mutableListOf(navigationLocationProvider.lastLocation!!.toPoint(), point))
|
||||||
|
.build(),
|
||||||
|
routesRequestCallback
|
||||||
|
)
|
||||||
|
true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,41 @@
|
||||||
package eu.ztsh.garmin.mapbox
|
package eu.ztsh.garmin.mapbox
|
||||||
|
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import com.mapbox.common.location.Location
|
||||||
import com.mapbox.navigation.core.MapboxNavigation
|
import com.mapbox.navigation.core.MapboxNavigation
|
||||||
import com.mapbox.navigation.core.lifecycle.MapboxNavigationObserver
|
import com.mapbox.navigation.core.lifecycle.MapboxNavigationObserver
|
||||||
|
import com.mapbox.navigation.core.trip.session.LocationMatcherResult
|
||||||
|
import com.mapbox.navigation.core.trip.session.LocationObserver
|
||||||
|
import com.mapbox.navigation.ui.maps.camera.data.MapboxNavigationViewportDataSource
|
||||||
|
|
||||||
class NavigationObserver : MapboxNavigationObserver {
|
class NavigationObserver(private val mapControl: MapControl) : MapboxNavigationObserver {
|
||||||
|
|
||||||
|
private val viewportDataSource = MapboxNavigationViewportDataSource(mapControl.mapView.mapboxMap)
|
||||||
|
|
||||||
|
private val locationObserver = object : LocationObserver {
|
||||||
|
|
||||||
|
override fun onNewLocationMatcherResult(locationMatcherResult: LocationMatcherResult) {
|
||||||
|
mapControl.navigationLocationProvider.changePosition(
|
||||||
|
location = locationMatcherResult.enhancedLocation,
|
||||||
|
keyPoints = locationMatcherResult.keyPoints
|
||||||
|
)
|
||||||
|
viewportDataSource.onLocationChanged(locationMatcherResult.enhancedLocation)
|
||||||
|
viewportDataSource.evaluate()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onNewRawLocation(rawLocation: Location) {
|
||||||
|
println()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
override fun onAttached(mapboxNavigation: MapboxNavigation) {
|
override fun onAttached(mapboxNavigation: MapboxNavigation) {
|
||||||
|
mapboxNavigation.registerLocationObserver(locationObserver)
|
||||||
Log.d(TAG, "Attached")
|
Log.d(TAG, "Attached")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDetached(mapboxNavigation: MapboxNavigation) {
|
override fun onDetached(mapboxNavigation: MapboxNavigation) {
|
||||||
|
mapboxNavigation.unregisterLocationObserver(locationObserver)
|
||||||
Log.d(TAG, "Detached")
|
Log.d(TAG, "Detached")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,4 @@
|
||||||
android:id="@+id/mapView"
|
android:id="@+id/mapView"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
mapbox:mapbox_locationComponentEnabled = "true"
|
|
||||||
mapbox:mapbox_locationComponentPuckBearingEnabled = "true"
|
|
||||||
tools:context=".MainActivity" />
|
tools:context=".MainActivity" />
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue