Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

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

When I call getMapAsync() on onViewCreated() of the fragment, OnMapReady callback is triggering and displaying the maps on the screen.

But I need my getMapAsync call inside a LocationCallBack since I need my current location before displaying the maps.

The weird thing is onMapReady callback is not triggering when first entering the fragment with huawei maps when getMapAsync() is called inside the locationCallback. But when I navigate to an another fragment then popbackstack back to the fragment with huawei maps, onMapReady suddenly triggers and displays the map like there was no problem.

Does anyone experienced this bug? It always occur when first entering the fragment with huawei maps

This is how my location callback looks like:

  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 && [email protected]) {

                    mSupportMapFragment?.getMapAsync(this@SampleMapsFragment)
                }

            }

            override fun onLocationAvailability(
                locationAvailability: LocationAvailability?
            ) {
            }
        }.also {
            locationCallback = it }

   locationClient
        ?: LocationServices.getFusedLocationProviderClient(requireActivity())
            .also {
                locationClient = it }

Then I have this:

private var locationClient: FusedLocationProviderClient? = null

  locationClient?.requestLocationUpdates(locationRequest, locationCallback, null)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

getMapAsync() should be initialized when the map is created. It is because initialization is not allowed during callback. If you want to obtain current location, and then display the map, you are advised to hide the map when you set it.

map


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...