4

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: enter image description here

Map when use moveCamera:enter image description here

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?

Master Zzzing
  • 524
  • 6
  • 18

2 Answers2

0

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

  • 1
    Thank brother but didn't work. I cleared application catch and restart the app. I saw only a gray screen. The map didn't load unless I start moving the map manually. – Master Zzzing Oct 04 '19 at 07:49
-1
  1. Try to change .builder(mMap.cameraPosition)... to .builder()...
  2. Make sure you are calling this method after onMapReady callback
Simon
  • 1,657
  • 11
  • 16
  • 1
    thank you for your answer. I tried but didn't work. yes, for sure this method can only be called after onMapReady() callback – Master Zzzing Oct 04 '19 at 06:39