android google maps not loading the map when using GoogleMap.AnimateCamera()

蹲街弑〆低调 提交于 2021-01-27 20:37:40

问题


Google Maps document

According to Google maps document, in order to apply a CameraUpdate to the map, we can either move the camera instantly(by using GoogleMap.moveCamera(CameraUpdate)) or animate the camera smoothly(by using GoogleMap.animateCamera(CameraUpdate)).

What I did

So I started by using GoogleMap.moveCamera(CameraUpdate). The map can be loaded just fine. However, when I used GoogleMap.animateCamera(CameraUpdate), the map can't be loaded. What I saw was just a gray screen or a blur map. The map would be fully loaded or became clear again unless I moved it manually.

Could anyone please tell me what is the problem? Does it require somethings else when working with GoogleMap.animateCamera()?

Updated: I just found a big mistake in my code and really sorry that I had not described it clear enough. I used GMap.animateCamera() to update the camera whenever the heading of the device changed(used rotation sensor...). This happens too fast so cameraAnimation() can never finish its work. that's why the map can't be fully loaded either.

onDeviceHeadingChange(){
   val cameraPosition = CameraPosition.builder(mMap.cameraPosition)
                                    .target(myLatLng)
                                    .bearing(myBearing)
                                    .tilt(50f)
                                    .build()
    val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition)
    // the map will be loaded just fine with this method
    // mMap.moveCamera(cameraUpdate)

    // the problem appeared when I update camera with aniteCamera()
    mMap.animateCamera(cameraUpdate, 500, null)
}

Map when use animateCamera:

Map when use moveCamera:

Solved

onDeviceHeadingChange(){
   if(!cameraIsMoving){
      cameraIsMoving = true
      val cameraPosition = CameraPosition.builder(mMap.cameraPosition)
                                    .target(myLatLng)
                                    .bearing(myBearing)
                                    .tilt(50f)
                                    .build()
       val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition)
       mMap.animateCamera(cameraUpdate, 500, object: GoogleMap.CancelableCallback{
                                    override fun onFinish() {
                                        cameraIsMoving = false
                                    }

                                    override fun onCancel() {
                                        cameraIsMoving = false
                                    }
                                })
    }
}

This take me to a new question Why GoogleMap.moveCamera() can handle those call? but GoogleMap.animateCamera() can't?


回答1:


  1. Try to change .builder(mMap.cameraPosition)... to .builder()...
  2. Make sure you are calling this method after onMapReady callback



回答2:


this works for me

LatLng initialLocation = new LatLng(latitude, longitude);
gmap.animateCamera(CameraUpdateFactory.newLatLngZoom(initialLocation, 18.0f));

I use it to animate map to a particular location with LatLng, I have never tried tilt though



来源:https://stackoverflow.com/questions/58230401/android-google-maps-not-loading-the-map-when-using-googlemap-animatecamera

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!