diff --git a/app/src/main/java/eu/ztsh/garmin/MainActivity.kt b/app/src/main/java/eu/ztsh/garmin/MainActivity.kt index c5f4911..a958c08 100644 --- a/app/src/main/java/eu/ztsh/garmin/MainActivity.kt +++ b/app/src/main/java/eu/ztsh/garmin/MainActivity.kt @@ -63,8 +63,8 @@ class MainActivity : AppCompatActivity() { } } MapboxNavigationApp.current()?.startTripSession() - mapControl = MapControl(binding.mapView, resources) - mapControl.follow() + mapControl = MapControl(this, binding.mapView, resources) + mapControl.init() } bluetoothInit() } diff --git a/app/src/main/java/eu/ztsh/garmin/mapbox/MapControl.kt b/app/src/main/java/eu/ztsh/garmin/mapbox/MapControl.kt index 4b919b6..255f95a 100644 --- a/app/src/main/java/eu/ztsh/garmin/mapbox/MapControl.kt +++ b/app/src/main/java/eu/ztsh/garmin/mapbox/MapControl.kt @@ -4,29 +4,60 @@ import android.content.res.Resources import android.util.Log import com.mapbox.maps.EdgeInsets import com.mapbox.maps.MapView -import com.mapbox.maps.plugin.viewport.data.FollowPuckViewportStateBearing +import com.mapbox.maps.Style +import com.mapbox.maps.plugin.gestures.gestures +import com.mapbox.maps.plugin.locationcomponent.location import com.mapbox.maps.plugin.viewport.data.FollowPuckViewportStateOptions import com.mapbox.maps.plugin.viewport.state.FollowPuckViewportState import com.mapbox.maps.plugin.viewport.viewport +import eu.ztsh.garmin.MainActivity -class MapControl(private val mapView: MapView, private val resources: Resources) { +class MapControl(private val context: MainActivity, private val mapView: MapView, private val resources: Resources) { - fun follow() { - val viewportPlugin = mapView.viewport - // transition to followPuckViewportState with default transition - val followPuckViewportState: FollowPuckViewportState = viewportPlugin.makeFollowPuckViewportState( - FollowPuckViewportStateOptions.Builder() + fun init() { + mapView.mapboxMap.loadStyle(Style.TRAFFIC_DAY) // TODO: base on sun position + follow(true) + setGestures(mapView) + } + + fun follow(immediately: Boolean = false) { + mapView.viewport.apply { + // transition to followPuckViewportState with default transition + val followPuckViewportState: FollowPuckViewportState = this.makeFollowPuckViewportState( + FollowPuckViewportStateOptions.Builder() // .bearing(FollowPuckViewportStateBearing.Constant(0.0)) - .padding(EdgeInsets(200.0 * resources.displayMetrics.density, 0.0, 0.0, 0.0)) - .build() - ) - val immediateTransition = viewportPlugin.makeImmediateViewportTransition() - viewportPlugin.transitionTo(followPuckViewportState, immediateTransition) { success -> - Log.d(TAG, "follow: $success") + .padding(EdgeInsets(200.0 * resources.displayMetrics.density, 0.0, 0.0, 0.0)) + .build() + ) + if (immediately) { + val immediateTransition = this.makeImmediateViewportTransition() + this.transitionTo(followPuckViewportState, immediateTransition) { success -> + Log.d(TAG, "follow: $success") + } + } else { + this.transitionTo(followPuckViewportState) { success -> + Log.d(TAG, "follow: $success") + } + } + } + } + + private fun setGestures(mapView: MapView) { + mapView.gestures.apply { + addOnMapClickListener { point -> + mapView.location.isLocatedAt(point) { isPuckLocatedAtPoint -> + if (isPuckLocatedAtPoint) { + follow() + } + } + true + } } } companion object { + const val TAG = "MAPCTRL" + } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index ae8744c..8bcf23b 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -6,5 +6,5 @@ android:layout_width="match_parent" android:layout_height="match_parent" mapbox:mapbox_locationComponentEnabled = "true" - mapbox:mapbox_locationComponentPuckBearing = "heading" + mapbox:mapbox_locationComponentPuckBearingEnabled = "true" tools:context=".MainActivity" />