【发布时间】:2021-01-11 06:54:23
【问题描述】:
当我在片段的 onViewCreated() 上调用 getMapAsync() 时,OnMapReady 回调正在触发并在屏幕上显示地图。
但我需要在 LocationCallBack 中调用 getMapAsync,因为在显示地图之前我需要当前位置。
奇怪的是,在 locationCallback 内部调用 getMapAsync() 时,首次进入带有华为地图的 Fragment 时不会触发 onMapReady 回调。但是当我导航到另一个片段然后popbackstack回到带有华为地图的片段时,onMapReady突然触发并显示地图就像没有问题一样。
有人遇到过这个错误吗?首次使用华为地图进入fragment时总是出现
这是我的位置回调的样子:
private var locationRequest: LocationRequest? = null
private var locationCallback: LocationCallback? = null
locationRequest
?: LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
.setInterval(30000)
.setFastestInterval(10000)
.setSmallestDisplacement(250.toFloat())
.also {
locationRequest = it }
locationCallback
?: object :
LocationCallback() {
override fun onLocationResult(
locationResult: LocationResult?
) {
locationResult
?: return
locationResult.lastLocation
?: return
currentLocation = LatLng(
locationResult.lastLocation.latitude, locationResult.lastLocation.longitude
)
// getMapAsync only if the map isn't initialized yet
if (huaweiMap == null && this@SampleMapsFragment.isAdded) {
mSupportMapFragment?.getMapAsync(this@SampleMapsFragment)
}
}
override fun onLocationAvailability(
locationAvailability: LocationAvailability?
) {
}
}.also {
locationCallback = it }
locationClient
?: LocationServices.getFusedLocationProviderClient(requireActivity())
.also {
locationClient = it }
然后我有这个:
private var locationClient: FusedLocationProviderClient? = null
locationClient?.requestLocationUpdates(locationRequest, locationCallback, null)
【问题讨论】:
-
getMapAsync真的打电话了吗?也许你有locationResult或locationResult.lastLocationnull并在调用getMapAsync之前返回? -
是的,我添加了一个断点和一个日志以确保,并且 getMapAsync 在 locationCallback 中被调用。
标签: android kotlin maps huawei-mobile-services huawei-developers