Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android setOnMyLocationChangeListener is deprecated

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

like image 953
Thiago Avatar asked Apr 06 '16 07:04

Thiago


3 Answers

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.

like image 57
IntelliJ Amiya Avatar answered Oct 31 '22 15:10

IntelliJ Amiya


FusedLocationProviderApi is now deprecated too. Try FusedLocationProviderClient.

like image 39
Mike H Avatar answered Oct 31 '22 15:10

Mike H


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)
like image 3
solamour Avatar answered Oct 31 '22 16:10

solamour