From eb93531816dfa7cb6966c1110a9459f637bdf695 Mon Sep 17 00:00:00 2001 From: clement Date: Sun, 7 Jul 2024 18:28:58 +0200 Subject: [PATCH] Re activate displaying speed and accuracy and get location name with latitude and longitude --- .idea/dbnavigator.xml | 404 ++++++++++++++++++ .../java/com/example/busroute/MainActivity.kt | 44 +- 2 files changed, 444 insertions(+), 4 deletions(-) create mode 100644 .idea/dbnavigator.xml diff --git a/.idea/dbnavigator.xml b/.idea/dbnavigator.xml new file mode 100644 index 0000000..90c3ece --- /dev/null +++ b/.idea/dbnavigator.xml @@ -0,0 +1,404 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/example/busroute/MainActivity.kt b/app/src/main/java/com/example/busroute/MainActivity.kt index 846d852..3de94f7 100644 --- a/app/src/main/java/com/example/busroute/MainActivity.kt +++ b/app/src/main/java/com/example/busroute/MainActivity.kt @@ -5,6 +5,8 @@ import android.content.ContentValues import android.content.Context import android.content.Intent import android.content.pm.PackageManager +import android.location.Address +import android.location.Geocoder import android.location.Location import android.location.LocationManager import android.net.Uri @@ -14,13 +16,16 @@ import android.provider.BaseColumns import android.util.Log import android.view.WindowManager import android.widget.Button +import android.widget.TextView import androidx.activity.ComponentActivity import androidx.core.app.ActivityCompat +import androidx.core.content.ContentProviderCompat.requireContext import androidx.core.content.ContextCompat import com.example.busroute.Database.DbHelper import com.example.busroute.Database.StopContract import com.google.android.gms.location.LocationCallback import com.google.android.gms.location.LocationRequest +import com.google.android.gms.location.LocationResult import org.osmdroid.api.IMapController import org.osmdroid.config.Configuration.getInstance import org.osmdroid.tileprovider.tilesource.TileSourceFactory @@ -31,7 +36,9 @@ import org.osmdroid.views.overlay.compass.CompassOverlay import org.osmdroid.views.overlay.compass.InternalCompassOrientationProvider import org.osmdroid.views.overlay.gestures.RotationGestureOverlay 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 class MainActivity : ComponentActivity() { @@ -55,6 +62,8 @@ class MainActivity : ComponentActivity() { super.onCreate(savedInstanceState) window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) val dbHelper = DbHelper(this) + val geocoder = Geocoder(this, Locale.getDefault()) + locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this)) @@ -140,10 +149,37 @@ class MainActivity : ComponentActivity() { val compassOverlay = CompassOverlay(this, InternalCompassOrientationProvider(this), map) compassOverlay.enableCompass() map.overlays.add(compassOverlay) - val mLocationOverlay = MyLocationNewOverlay(GpsMyLocationProvider(this), this.map) - mLocationOverlay.enableMyLocation() - mLocationOverlay.enableFollowLocation() - map.overlays.add(mLocationOverlay) + val locationOverlay = + object : MyLocationNewOverlay(GpsMyLocationProvider(this), map) { + override fun onLocationChanged(location: Location?, source: IMyLocationProvider?) { + super.onLocationChanged(location!!, source) + if (location.accuracy < 60) { + if (location.provider == LocationManager.NETWORK_PROVIDER) { + location.provider = LocationManager.GPS_PROVIDER + } else { + location.provider = LocationManager.NETWORK_PROVIDER + } + } + currentLocation = location + latitude = location.latitude + longitude = location.longitude + mapController.animateTo(GeoPoint(latitude, longitude)) + + findViewById(R.id.accuracy).text = "${location.accuracy} %" + findViewById(R.id.position).text = "$latitude $longitude" + findViewById(R.id.speed).text = "${location.speed} m/s" + findViewById(R.id.speed_accuracy).text = + "${location.speedAccuracyMetersPerSecond} %" + + Log.i("Position", "$latitude $longitude") + Log.i("Accuracy Chosen", "${location.provider}") + var geoResults: MutableList
? = geocoder.getFromLocation(latitude, longitude, 1) + Log.i("Location", geoResults.toString()) + } + } + locationOverlay.enableMyLocation() + locationOverlay.enableFollowLocation() + map.overlays.add(locationOverlay) isLocationPermissionGranted() // val locationClient = LocationServices.getFusedLocationProviderClient(this)