update location when gps changed

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2021-06-08 20:19:33 +02:00 committed by Andy Scherzinger
parent 751cd1268e
commit 0568ea6e72
No known key found for this signature in database
GPG key ID: 6CADC7E3523C308B

View file

@ -5,6 +5,9 @@ import android.app.SearchManager
import android.content.Context import android.content.Context
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.graphics.drawable.ColorDrawable import android.graphics.drawable.ColorDrawable
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.text.InputType import android.text.InputType
@ -74,6 +77,7 @@ import javax.inject.Inject
class LocationPickerController(args: Bundle) : class LocationPickerController(args: Bundle) :
BaseController(args), BaseController(args),
SearchView.OnQueryTextListener, SearchView.OnQueryTextListener,
LocationListener,
GeocodingController.GeocodingResultListener { GeocodingController.GeocodingResultListener {
@Inject @Inject
@ -114,6 +118,10 @@ class LocationPickerController(args: Bundle) :
var roomToken: String? var roomToken: String?
var myLocation: GeoPoint = GeoPoint(0.0, 0.0)
private var locationManager: LocationManager? = null
private lateinit var locationOverlay: MyLocationNewOverlay
var moveToCurrentLocationWasClicked: Boolean = true var moveToCurrentLocationWasClicked: Boolean = true
var readyToShareLocation: Boolean = false var readyToShareLocation: Boolean = false
var searchItem: MenuItem? = null var searchItem: MenuItem? = null
@ -141,6 +149,16 @@ class LocationPickerController(args: Bundle) :
initMap() initMap()
} }
override fun onDetach(view: View) {
super.onDetach(view)
try {
locationManager!!.removeUpdates(this)
} catch (e: Exception) {
Log.e(TAG, "error when trying to remove updates for location Manager", e)
}
locationOverlay.disableMyLocation()
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater) super.onCreateOptionsMenu(menu, inflater)
inflater.inflate(R.menu.menu_locationpicker, menu) inflater.inflate(R.menu.menu_locationpicker, menu)
@ -217,14 +235,19 @@ class LocationPickerController(args: Bundle) :
map?.onResume() map?.onResume()
locationManager = activity!!.getSystemService(Context.LOCATION_SERVICE) as LocationManager
try {
locationManager!!.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0L, 0f, this)
} catch (ex: SecurityException) {
}
val copyrightOverlay = CopyrightOverlay(context) val copyrightOverlay = CopyrightOverlay(context)
map?.overlays?.add(copyrightOverlay) map?.overlays?.add(copyrightOverlay)
map?.setMultiTouchControls(true) map?.setMultiTouchControls(true)
map?.isTilesScaledToDpi = true map?.isTilesScaledToDpi = true
val locationOverlay = MyLocationNewOverlay(GpsMyLocationProvider(context), map) locationOverlay = MyLocationNewOverlay(GpsMyLocationProvider(context), map)
// locationOverlay.enableFollowLocation()
locationOverlay.enableMyLocation() locationOverlay.enableMyLocation()
locationOverlay.setPersonHotspot(20.0F,20.0F) locationOverlay.setPersonHotspot(20.0F,20.0F)
locationOverlay.setPersonIcon( locationOverlay.setPersonIcon(
@ -239,13 +262,10 @@ class LocationPickerController(args: Bundle) :
mapController?.setZoom(12.0) mapController?.setZoom(12.0)
} }
var myLocation: GeoPoint val zoomToCurrentPositionOnFirstFix = !receivedChosenGeocodingResult
myLocation = GeoPoint(52.0, 13.0)
var zoomToCurrentPositionAllowed = !receivedChosenGeocodingResult
locationOverlay.runOnFirstFix { locationOverlay.runOnFirstFix {
myLocation = locationOverlay.myLocation myLocation = locationOverlay.myLocation
if (zoomToCurrentPositionAllowed) { if (zoomToCurrentPositionOnFirstFix) {
activity!!.runOnUiThread { activity!!.runOnUiThread {
mapController?.setZoom(12.0) mapController?.setZoom(12.0)
mapController?.setCenter(myLocation) mapController?.setCenter(myLocation)
@ -433,6 +453,22 @@ class LocationPickerController(args: Bundle) :
} }
} }
override fun onLocationChanged(location: Location?) {
myLocation = GeoPoint(location)
}
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
// empty
}
override fun onProviderEnabled(provider: String?) {
// empty
}
override fun onProviderDisabled(provider: String?) {
// empty
}
companion object { companion object {
private const val TAG = "LocPicker" private const val TAG = "LocPicker"
private const val REQUEST_PERMISSIONS_REQUEST_CODE = 1 private const val REQUEST_PERMISSIONS_REQUEST_CODE = 1