我使用这个类来为我的地图应用程序获取设备的当前位置。我正在使用它并且它工作正常,但如果设备上不可用GooglePlayServices
,我最近切换到HMS
华为设备。GooglePlayServices
我用导入的 libGooglePlayServices
中的镜像对象替换了所有类,HMS
并且编译没有错误。但是当我要求当前位置时,它不会返回任何东西。没有例外,没有成功或失败。
我没有收到回调onLocationResult()
或catch()
阻止。根据调试器最后一行调用是val task = lp.requestLocationUpdates(lr, this, Looper.getMainLooper())
有人有这个问题吗?这显然是一个新问题。在不可Huawei P40
用的地方GooglePlayServices
进行测试。
此外,HuaweiMap 不在发布模式下工作。getMapAsync()
不会返回onMapReady()
回调。它卡在那里。但是如果我切换调试模式,它就可以正常工作。
UDPATE: HuaweiMap 正在运行。更新了proguard。但是位置仍然无法正常工作。即使在调试模式下它也不起作用。
代码:
private inner class LocationCbHua(val lp: com.huawei.hms.location.FusedLocationProviderClient,
val onFailure: (()->Unit)? = null,
val onSuccess: (GpsLocation)->Unit)
: com.huawei.hms.location.LocationCallback() {
init {
val lr = com.huawei.hms.location.LocationRequest.create().apply {
priority = com.huawei.hms.location.LocationRequest.PRIORITY_HIGH_ACCURACY
interval = 200
}
val lsr = com.huawei.hms.location.LocationSettingsRequest.Builder().run {
// setAlwaysShow(true) // TEST
addLocationRequest(lr)
build()
}
val check = com.huawei.hms.location.LocationServices.getSettingsClient(activity!!).checkLocationSettings(lsr)
check.addOnCompleteListener {
try {
check.getResultThrowException(com.huawei.hms.common.ApiException::class.java)
val task = lp.requestLocationUpdates(lr, this, Looper.getMainLooper())
task.addOnFailureListener {
onFailure?.invoke()
}
} catch (e: com.huawei.hms.common.ApiException) {
when (e.statusCode) {
com.huawei.hms.location.LocationSettingsStatusCodes.RESOLUTION_REQUIRED-> if(!locationResolutionAsked){
// Location settings are not satisfied. But could be fixed by showing the user a dialog.
try {
// Cast to a resolvable exception.
val re = e as com.huawei.hms.common.ResolvableApiException
// Show the dialog by calling startResolutionForResult(), and check the result in onActivityResult().
re.startResolutionForResult(mainActivity, MainActivity.REQUEST_LOCATION_SETTINGS)
locationResolutionAsked = true
} catch (e: Exception) {
e.printStackTrace()
}
}
com.huawei.hms.location.LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE->{
// Location settings are not satisfied. However, we have no way to fix the settings so we won't show the dialog.
App.warn("Location is not available")
onFailure?.invoke()
}
}
}
}
}
fun cancel(){
lp.removeLocationUpdates(this)
currLocCb = null
}
override fun onLocationResult(lr: com.huawei.hms.location.LocationResult) {
cancel()
val ll = lr.lastLocation
onSuccess(GpsLocation(ll.longitude, ll.latitude))
}
}