是的,LocationRequest构造函数已经被弃用。您可以使用它的静态方法LocationRequest.create()
来创建一个位置请求。
Kotlin:
locationRequest = LocationRequest.create().apply {
interval = 100
fastestInterval = 50
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
maxWaitTime = 100
}
Java:
locationRequest = LocationRequest.create()
.setInterval(100)
.setFastestInterval(3000)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setMaxWaitTime(100);
更新
正如@Shimon所指出的那样,LocationRequest.PRIORITY_HIGH_ACCURACY现在已经被弃用,因此请使用Priority.PRIORITY_HIGH_ACCURACY
LocationRequest.create().apply{ ... }
现在也已被弃用。
请改用LocationRequest.Builder()
。例如:
(locationInterval
、locationFastestInterval
和locationMaxWaitTime
对应于以前使用create()
时使用的值)
locationRequest = LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, locationInterval)
.setWaitForAccurateLocation(false)
.setMinUpdateIntervalMillis(locationFastestInterval)
.setMaxUpdateDelayMillis(locationMaxWaitTime)
.build()
请阅读更多内容:https://developer.android.com/reference/android/location/LocationRequest.Builder和https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.Builder。LocationRequest.Builder(...)
前加上 new
。 - Anatol BivolBuilder
只有一个参数,请查看答案中添加的链接。 - Konstantin Konopko这行代码已被弃用: priority = LocationRequest.PRIORITY_HIGH_ACCURACY
替换成 priority = Priority.PRIORITY_HIGH_ACCURACY
private LocationRequest locationRequest;
在 onCreate() 方法中
locationRequest = new LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 100)
.setWaitForAccurateLocation(false)
.setMinUpdateIntervalMillis(2000)
.setMaxUpdateDelayMillis(100)
.build();
更新:
常量PRIORITY_HIGH_ACCURACY
已弃用,使用Priority.PRIORITY_HIGH_ACCURACY
代替。
private fun updateLocationTracking(isTracking: Boolean) {
if(isTracking) {
if(TrackingUtility.hasLocationPermissions(this)) {
val request = LocationRequest.create().apply {
interval = LOCATION_UPDATE_INTERVAL
fastestInterval = FASTEST_LOCATION_INTERVAL
priority = Priority.PRIORITY_HIGH_ACCURACY
}
}
import com.google.android.gms.location.LocationRequest
locationRequest = LocationRequest.Builder(1000L).setPriority(Priority.PRIORITY_HIGH_ACCURACY).build()
至少有两种不同的LocationRequests
本文讨论的是谷歌地图,但对于那些寻找谷歌地图之外的位置更新的人们,最近也有一些变化。
使用setQuality(LocationRequest.QUALITY_HIGH_ACCURACY)设置HIGH_ACCURACY。
val fastLocationRequests = LocationRequest.Builder(1000L) // interval specified in millis so this is every 1 second
.setQuality(LocationRequest.QUALITY_HIGH_ACCURACY)
.setMaxUpdateDelayMillis(0L) // Note 0 represents no batching allowed
.build()
使用替代的Builder()
val slowLocationRequests = LocationRequest.Builder(fastLocationRequests)
.setIntervalMillis(60000L) // once per minute
.build()
LocationRequest locationRequest = LocationRequest.create() //if you want access of variable
.setInterval(100)
.setFastestInterval(3000)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setNumUpdates(1)
.setMaxWaitTime(100);
locationRequest = new LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, locationInterval) .setWaitForAccurateLocation(false) .setMinUpdateIntervalMillis(locationFastestInterval) .setMaxUpdateDelayMillis(locationMaxWaitTime) .build();
在此之前先声明所有变量。
LocationRequest.create()
现已过时,您应该使用LocationRequest.Builder()
。 - Joaquin Iurchukimplementation "com.google.android.gms:play-services-location:21.0.0"
。这样,您就不应该使用 API 31 中的 LocationBuilder,而是应该使用 Play Services。 - Joaquin Iurchuk