From f18c396d642943f0f1e6cefa8ed782692c1b227b Mon Sep 17 00:00:00 2001 From: clement Date: Thu, 18 Jul 2024 17:38:53 +0200 Subject: [PATCH] Remove compass and track bearing --- .../java/com/example/busroute/MainActivity.kt | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/example/busroute/MainActivity.kt b/app/src/main/java/com/example/busroute/MainActivity.kt index 69a917c..817b858 100644 --- a/app/src/main/java/com/example/busroute/MainActivity.kt +++ b/app/src/main/java/com/example/busroute/MainActivity.kt @@ -37,6 +37,7 @@ import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider import org.osmdroid.views.overlay.mylocation.IMyLocationProvider import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay import java.util.Locale +import kotlin.math.abs class MainActivity : ComponentActivity() { @@ -44,6 +45,7 @@ class MainActivity : ComponentActivity() { private lateinit var map : MapView private lateinit var currentLocation: Location + private var lastBearing = 0.0f private lateinit var locationManager: LocationManager private lateinit var mapController: IMapController private lateinit var positionMarker: Marker @@ -69,19 +71,6 @@ class MainActivity : ComponentActivity() { Voice.LATENCY_LOW, true, null)) tts.setSpeechRate(0.8f) - - val mySensorEventListener: SensorListener = object : SensorListener { - override fun onSensorChanged(sensor: Int, values: FloatArray) { - val mHeading = values[0] - if(navigationEnable){ - map.mapOrientation = -mHeading - } - } - - override fun onAccuracyChanged(p0: Int, p1: Int) { - } - } - locationManager = getSystemService(LOCATION_SERVICE) as LocationManager getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this)) setContentView(R.layout.main) @@ -139,9 +128,9 @@ class MainActivity : ComponentActivity() { rotationGestureOverlay.isEnabled map.setMultiTouchControls(true) map.overlays.add(rotationGestureOverlay) - val compassOverlay = CompassOverlay(this, InternalCompassOrientationProvider(this), map) - compassOverlay.enableCompass() - map.overlays.add(compassOverlay) +// val compassOverlay = CompassOverlay(this, InternalCompassOrientationProvider(this), map) +// compassOverlay.enableCompass() +// map.overlays.add(compassOverlay) val locationOverlay = object : MyLocationNewOverlay(GpsMyLocationProvider(this), map) { override fun onLocationChanged(location: Location?, source: IMyLocationProvider?) { @@ -156,7 +145,13 @@ class MainActivity : ComponentActivity() { currentLocation = location latitude = location.latitude longitude = location.longitude - mapController.animateTo(GeoPoint(latitude, longitude)) + if(navigationEnable && currentLocation.hasBearing()){ + lastBearing = currentLocation.bearing + mapController.animateTo(GeoPoint(latitude, longitude), null, null, -currentLocation.bearing) + } + else{ + mapController.animateTo(GeoPoint(latitude, longitude)) + } findViewById(R.id.accuracy).text = "${location.accuracy} %" }