Hier I gave one example of getting the last known location with FusedLocationProviderClient
Now here is example with LocationManager:
Example with
trailing lambdas:
val locationManager = getSystemService(LOCATION_SERVICE) as LocationManager
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
1000L,
0f
) { location ->
logger.log("\nLat: " + location.latitude.toString()
+ "\nLon: " + location.longitude.toString()
+ "\nAltitude" + location.altitude.toString())
}
but in order to be able to stop requests, declare lamba like
function type:
private lateinit var gpsListener: LocationListener
...
gpsListener = LocationListener { location ->
logger.log(
"\nLat: " + location.latitude.toString()
+ "\nLon: " + location.longitude.toString()
+ "\nAltitude" + location.altitude.toString()
)
}
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
1000L,
0f,
gpsListener
)
Stop it like this:
locationManager.removeUpdates(gpsListener)
Example download from
here.