25

Android Google Map's setOnMyLocationChangeListener method is now deprecated. Does anyone know how to go around it? Thanks.

llmora
  • 563
  • 5
  • 11
Thiago
  • 12,778
  • 14
  • 93
  • 110

3 Answers3

14

setOnMyLocationChangeListener method is Deprecated now.

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
  • 23
    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. – swooby Sep 16 '16 at 19:26
  • 14
    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 – 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
3

FusedLocationProviderApi is now deprecated too. Try FusedLocationProviderClient.

Mike H
  • 160
  • 9