Android setOnMyLocationChangeListener is deprecated
Asked Answered
P

3

25

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

Pilau answered 6/4, 2016 at 7:18 Comment(1)
I love coward down votes without a comment whyTriliteral
P
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.

Protuberance answered 6/4, 2016 at 7:21 Comment(2)
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 problemsMeasures
G
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)
Glossography answered 26/5, 2020 at 23:30 Comment(3)
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
M
3

FusedLocationProviderApi is now deprecated too. Try FusedLocationProviderClient.

Mycosis answered 19/6, 2018 at 23:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.