Huawei Map and Location Kit - 在 LocationCallback 中调用 getMapAsync() 时出现错误

Huawei Map and Location Kit - Bug when getMapAsync() is called inside LocationCallback

当我在片段的 onViewCreated() 上调用 getMapAsync() 时,OnMapReady 回调会触发并在屏幕上显示地图。

但是我需要在 LocationCallBack 中调用 getMapAsync,因为在显示地图之前我需要我的当前位置。

奇怪的是,在locationCallback中调用getMapAsync()时,第一次进入带有华为地图的fragment时,onMapReady回调没有触发。但是当我导航到另一个片段然后popbackstack回到带有华为地图的片段时,onMapReady突然触发并显示地图就像没有问题一样。

有人遇到过这个错误吗?第一次进入华为地图片段时总是出现

这是我的位置回调的样子:

  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() should be initialized when the map is created。因为回调时不允许初始化。如果想获取当前位置,然后显示地图,建议设置时隐藏地图。