传统写法
suspend fun requestLocation(context: Context) = withContext(Dispatchers.IO) {
val lock = CountDownLatch(1)
var mLocation: AMapLocation? = null
val callBack = object : LocationManager.ILocationCallback {
override fun locSuccess(location: AMapLocation?) {
mLocation = location
lock.countDown()
}
override fun locFail(errorCode: Int, errorInfo: String) {
lock.countDown()
}
}
try {
LocationManager(callBack).location(context)
lock.await()
} catch (e: Exception) {
}
if (mLocation != null) {
cLocation = mLocation
}
mLocation
}
优雅写法
suspend fun requestLocation(context: Context) = suspendCoroutine<AMapLocation?> {
LocationManager(object : LocationMa