Android Google Map's setOnMyLocationChangeListener method is now deprecated. Does anyone know how to go around it? Thanks.
Android setOnMyLocationChangeListener is deprecated
Asked Answered
I love coward down votes without a comment why –
Triliteral
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.
Sure, that is what the docs say, but I think the intention of the OP is that they are currently using OnMyLocationChangeListener.onMyLocationChange to get a callback when the blue dot moves on the map, and they want to find a non-deprecated way to continue to do this...and there is none. The non-deprecated way is to write your own code to manually start and stop your own FusedLocationApi location updates, which is IMO stupid since your parameters may not be the same as the one used by the Map, and so you may not get updates at the same time that the blue dot moves. –
Canary
I agree with swooby, they removed a very easy way to get my location with updates of movement on the map, as well as map.getMyLocation() is also deprecated. Now Google is forcing us to write our own code and "selling us" the idea that the googleplay services are the best for the developers, when they are only giving us more and more problems –
Measures
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)
Or you can just quickly get user location like this : fusedLocationProviderClient?.lastLocation?.addOnSuccessListener { userLocation -> } –
Strangulate
any solution in java? –
Uredo
developer.android.com/training/location/request-updates#java shows the same thing in Java. –
Glossography
FusedLocationProviderApi is now deprecated too. Try FusedLocationProviderClient.
© 2022 - 2024 — McMap. All rights reserved.