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)
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.