gpt4 book ai didi

android - 如何同步获取当前地理位置?

转载 作者:太空宇宙 更新时间:2023-11-03 10:37:49 25 4
gpt4 key购买 nike

由于我们应用程序的工作方式,我需要同步获取用户的当前位置。我们当前的实现使用 com.google.android.gms.location.LocationListener 来接收位置更新。问题是,我需要在尝试第一次调用之前更新位置服务器端,否则用户会得到错误的数据。

LocationServices.FusedLocationApi.getLastLocation() 不适合这个,因为 a) 它似乎总是返回 null(无论出于何种原因)和 b) 我们已经保存了用户最后已知的位置服务器-side,因此检索最后已知位置并将其发送到服务器是多余的。

伪代码

//this is the first call I need to make
webservice.appStart(AppStartBody body);
//then I want to retrieve the current location and send it to the server
webservice.setGeoLocation(getCurrentLocation());
//finally, I retrieve the items on the server based on the location
webservice.getListItems();

等待第一个位置更新事件是我想避免的一种可能性,因为我不知道它会在多长时间内触发,我可能不得不让我的用户在加载屏幕中停留很长时间,然后失去他们,因为没有人喜欢等待。

最佳答案

我能够通过将检索位置的过程放在 Rx 可观察对象中来解决这个问题,特别是 SingleSingle 对象的 blockingGet 方法被调用以同步检索位置。然后将其放置在 try catch block 中以执行重试,因为在第一次尝试期间该位置并不总是可用。

(我知道这是一个老问题,但无论如何我都会发布一个答案,这样我就可以分享我是如何做到的。很抱歉使用 Kotlin 和 RxJava!但我想每个人都会明白我的要点想法并能够按照他们喜欢的方式实现它。我也在使用最新的 Google Location API。)

// Use an executor to prevent blocking the thread where 
// this method will be called. Also, DO NOT call this on
// the main thread!!!
fun tryRetrieveLocationSync(flc: FusedLocationProviderClient,
executor: Executor, numOfRetries: Int = 3,
retryWaitTime: Long = 500): Location {

var location: Location
var i = 1

while (true) {
try {
// Place the method call inside a try catch block
// because `Single#blockingGet` will throw any exception
// that was provided to the `Emitter#onError` as an argument.

location = retrieveLocationSync(flc, executor)
} catch (e: NoLocationDataException) {
if (i <= numOfRetries) {
// The value from the `FusedLocationProviderClient#lastLocation`
// task usually becomes available after less than second (tried
// and tested!!), but it's really up to you.

SystemClock.sleep(retryWaitTime * i)
i++
} else {
throw e // Give up once all the retries have been used.
}
} catch (e: Exception) {
// Rethrow anything else that was thrown from
// the `Single#blockingGet`.

throw e
}
}

return location
}

private fun retrieveLocationSync(flc: FusedLocationProviderClient,
executor: Executor): Location {

return Single.create<Location> { emitter ->
val task = flc.lastLocation
task.addOnCompleteListener(executor, OnCompleteListener { // it ->
if (it.isSuccessful) {
if (it.result != null) {
emitter.onSuccess(it.result)
} else {
// There is no location data available probably because
// the location services has just been enabled, the device
// has just been turned on, or no other applications has
// requested the device's location.
emitter.onError(NoLocationDataException())
}
} else {
// I haven't encountered any exception here but this is
// just to make sure everything's catchable.
emitter.onError(it.exception ?: RuntimeException())
}
})
}.blockingGet()
}

关于android - 如何同步获取当前地理位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40220025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com