feat: More options to map initialization

This commit is contained in:
Piotr Dec 2024-07-15 01:52:16 +02:00
parent 756b760d5c
commit 9db0e3611c
Signed by: stawros
GPG key ID: F89F27AD8F881A91
3 changed files with 47 additions and 16 deletions

View file

@ -63,8 +63,8 @@ class MainActivity : AppCompatActivity() {
} }
} }
MapboxNavigationApp.current()?.startTripSession() MapboxNavigationApp.current()?.startTripSession()
mapControl = MapControl(binding.mapView, resources) mapControl = MapControl(this, binding.mapView, resources)
mapControl.follow() mapControl.init()
} }
bluetoothInit() bluetoothInit()
} }

View file

@ -4,29 +4,60 @@ import android.content.res.Resources
import android.util.Log import android.util.Log
import com.mapbox.maps.EdgeInsets import com.mapbox.maps.EdgeInsets
import com.mapbox.maps.MapView 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.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
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() { fun init() {
val viewportPlugin = mapView.viewport 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 // transition to followPuckViewportState with default transition
val followPuckViewportState: FollowPuckViewportState = viewportPlugin.makeFollowPuckViewportState( val followPuckViewportState: FollowPuckViewportState = this.makeFollowPuckViewportState(
FollowPuckViewportStateOptions.Builder() FollowPuckViewportStateOptions.Builder()
// .bearing(FollowPuckViewportStateBearing.Constant(0.0)) // .bearing(FollowPuckViewportStateBearing.Constant(0.0))
.padding(EdgeInsets(200.0 * resources.displayMetrics.density, 0.0, 0.0, 0.0)) .padding(EdgeInsets(200.0 * resources.displayMetrics.density, 0.0, 0.0, 0.0))
.build() .build()
) )
val immediateTransition = viewportPlugin.makeImmediateViewportTransition() if (immediately) {
viewportPlugin.transitionTo(followPuckViewportState, immediateTransition) { success -> val immediateTransition = this.makeImmediateViewportTransition()
this.transitionTo(followPuckViewportState, immediateTransition) { success ->
Log.d(TAG, "follow: $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 { companion object {
const val TAG = "MAPCTRL" const val TAG = "MAPCTRL"
} }
} }

View file

@ -6,5 +6,5 @@
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_locationComponentEnabled = "true"
mapbox:mapbox_locationComponentPuckBearing = "heading" mapbox:mapbox_locationComponentPuckBearingEnabled = "true"
tools:context=".MainActivity" /> tools:context=".MainActivity" />