当我在 fragment 的 onViewCreated() 上调用 getMapAsync() 时,OnMapReady 回调正在触发并在屏幕上显示 map 。
但是我需要在 LocationCallBack 中调用 getMapAsync ,因为在显示 map 之前我需要当前位置。
奇怪的是,当在 locationCallback 中调用 getMapAsync() 时,首次进入带有华为 map 的 Fragment 时不会触发 onMapReady 回调。但是当我导航到另一个 fragment 然后popbackstack回到带有华为 map 的 fragment 时,onMapReady突然触发并显示 map 就像没有问题一样。
有没有人经历过这个错误?首次使用华为 map 进入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 () 应在 the map is created 时初始化.这是因为回调期间不允许初始化。如果要获取当前位置,然后显示 map ,建议设置时隐藏 map 。
关于android - 华为 map 和位置套件 - 在 LocationCallback 中调用 getMapAsync() 时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65662495/