Android Google Map's setOnMyLocationChangeListener method is now deprecated. Does anyone know how to go around it? Thanks.
Asked
Active
Viewed 1.6k times
25
-
7I love coward down votes without a comment why – alex Apr 09 '16 at 10:14
3 Answers
14
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.

IntelliJ Amiya
- 74,896
- 15
- 165
- 198
-
23Sure, 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. – swooby Sep 16 '16 at 19:26
-
14I 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 – zapotec Oct 13 '16 at 15:06
4
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)

solamour
- 2,764
- 22
- 22
-
Or you can just quickly get user location like this : fusedLocationProviderClient?.lastLocation?.addOnSuccessListener { userLocation -> } – Eugene P. Jan 20 '21 at 12:21
-
-
https://developer.android.com/training/location/request-updates#java shows the same thing in Java. – solamour Oct 07 '22 at 03:29