Android Google Map's setOnMyLocationChangeListener method is now deprecated. Does anyone know how to go around it? Thanks.
setOnMyLocationChangeListener
method is now.Deprecated
You can use com.google.android.gms.location.FusedLocationProviderApi
instead.
FusedLocationProviderApi which is the latest API and the best among the available possibilities to get location in Android.
FusedLocationProviderApi is now deprecated too. Try FusedLocationProviderClient.
Request location updates (https://developer.android.com/training/location/request-updates) explains the steps. In short,
1) Define variables.
val fusedLocationProviderClient by lazy {
LocationServices.getFusedLocationProviderClient(requireContext())
}
val locationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult?) {
locationResult ?: return
for (location in locationResult.locations){
moveToLocation(location)
}
}
}
val locationRequest = LocationRequest.create().apply {
interval = 10_000
fastestInterval = 5_000
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}
2) Request location updates. Make sure you get the location permission beforehand.
fusedLocationProviderClient.requestLocationUpdates(
locationRequest,
locationCallback,
Looper.getMainLooper()
)
3) When you are done, remove updates.
fusedLocationProviderClient.removeLocationUpdates(locationCallback)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With