java 不推荐使用 Android setOnMyLocationChangeListener

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36444085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:27:38  来源:igfitidea点击:

Android setOnMyLocationChangeListener is deprecated

javaandroidgoogle-mapsandroid-maps-v2

提问by Joolah

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

Android Google Map 的setOnMyLocationChangeListener方法现已弃用。有谁知道如何绕过它?谢谢。

回答by IntelliJ Amiya

setOnMyLocationChangeListenermethod is Deprecatednow.

setOnMyLocationChangeListener方法是Deprecated现在。

You can use com.google.android.gms.location.FusedLocationProviderApiinstead.

你可以 com.google.android.gms.location.FusedLocationProviderApi改用。

FusedLocationProviderApi which is the latest API and the best among the available possibilities to get location in Android.

FusedLocationProviderApi 这是最新的 API,也是在 Android 中获取位置的最佳方法。

回答by Mike H

回答by solamour

Request location updates (https://developer.android.com/training/location/request-updates) explains the steps. In short,

请求位置更新 ( https://developer.android.com/training/location/request-updates) 解释了这些步骤。简而言之,

1) Define variables.

1) 定义变量。

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.

2) 请求位置更新。确保事先获得位置许可。

fusedLocationProviderClient.requestLocationUpdates(
    locationRequest,
    locationCallback,
    Looper.getMainLooper()
)

3) When you are done, remove updates.

3) 完成后,删除更新。

fusedLocationProviderClient.removeLocationUpdates(locationCallback)