Thiago
Thiago

Reputation: 13302

Android setOnMyLocationChangeListener is deprecated

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

Upvotes: 25

Views: 16116

Answers (3)

solamour
solamour

Reputation: 3224

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)

Upvotes: 4

Mike H
Mike H

Reputation: 160

FusedLocationProviderApi is now deprecated too. Try FusedLocationProviderClient.

Upvotes: 3

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

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.

Upvotes: 14

Related Questions