11package org.owntracks.android.ui.map.osm
22
3+ import android.content.Context
34import android.graphics.Bitmap
45import android.graphics.drawable.BitmapDrawable
56import android.location.Location
67import android.os.Bundle
78import android.view.LayoutInflater
89import android.view.MotionEvent.ACTION_BUTTON_RELEASE
10+ import android.view.Surface
911import android.view.View
1012import android.view.ViewGroup
1113import androidx.core.content.res.ResourcesCompat
@@ -19,7 +21,6 @@ import org.osmdroid.events.MapListener
1921import org.osmdroid.events.ScrollEvent
2022import org.osmdroid.events.ZoomEvent
2123import org.osmdroid.tileprovider.tilesource.TileSourceFactory
22- import org.osmdroid.util.GeoPoint
2324import org.osmdroid.views.CustomZoomButtonsController
2425import org.osmdroid.views.MapView
2526import org.osmdroid.views.overlay.CopyrightOverlay
@@ -143,14 +144,25 @@ class OSMMapFragment internal constructor(
143144 }
144145 })
145146
146- class MapRotationOrientationProvider : IOrientationProvider {
147+ class MapRotationOrientationProvider (private val context : Context ) : IOrientationProvider {
148+ val display = context.display
147149 private var myOrientationConsumer: IOrientationConsumer ? = null
148150 private var lastOrientation = 0f
149151 fun updateOrientation (orientation : Float ) {
150- lastOrientation = orientation
151- myOrientationConsumer?.onOrientationChanged(- orientation, this )
152+ val correctedOrientation = orientation
153+ lastOrientation = - (correctedOrientation + displayRotationToDegrees())
154+ myOrientationConsumer?.onOrientationChanged(lastOrientation, this )
152155 }
153156
157+ private fun displayRotationToDegrees (): Float =
158+ when (display?.rotation) {
159+ Surface .ROTATION_0 -> 0f
160+ Surface .ROTATION_90 -> 90f
161+ Surface .ROTATION_180 -> 180f
162+ Surface .ROTATION_270 -> 270f
163+ else -> 0f
164+ }
165+
154166 override fun startOrientationProvider (orientationConsumer : IOrientationConsumer ? ): Boolean {
155167 myOrientationConsumer = orientationConsumer
156168 return true
@@ -164,7 +176,7 @@ class OSMMapFragment internal constructor(
164176 override fun destroy () {}
165177 }
166178
167- val orientationProvider = MapRotationOrientationProvider ()
179+ val orientationProvider by lazy { MapRotationOrientationProvider (requireContext()) }
168180 val compassOrientationMapListener = object : MapListener {
169181 private fun updateOrientation () {
170182 mapView?.mapOrientation?.run {
@@ -326,6 +338,17 @@ class OSMMapFragment internal constructor(
326338 super .onDetach()
327339 }
328340
341+ /* *
342+ * This gets fired on rotate. We need to trigger an onScroll event to reset the orientation
343+ * provider and re-draw the compass
344+ *
345+ * @param newConfig
346+ */
347+ override fun onConfigurationChanged (newConfig : android.content.res.Configuration ) {
348+ super .onConfigurationChanged(newConfig)
349+ compassOrientationMapListener.onScroll(null )
350+ }
351+
329352 override fun drawRegions (regions : Set <WaypointModel >) {
330353 if (preferences.showRegionsOnMap) {
331354 mapView?.run {
0 commit comments