Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Maps - animateCamera() method not working proper

Problem:

1) Map getting animated to reach the required location(4th line in code) but it got zoomed to the default location(5th line in code)

[leaving the map in the default location at the specified zoom level]

2) I understand why is the problem happening but i don't know how to resolve it.

3) If i change the 4th line to moveCamera instead of animateCamera that will work, but i do want animateCamera() method.

Here's the code:

map=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
MarkerOptions options=new MarkerOptions().position(new LatLng(13.0810,80.2740));
map.addMarker(options);
map.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(13.0810,80.2740)),4000,null);
map.animateCamera(CameraUpdateFactory.zoomTo(15.5f),2000,null);
like image 590
Usual Suspect Avatar asked May 28 '13 16:05

Usual Suspect


2 Answers

The problem is that you call zoom right after you started animating to the new location. That's why it just replaces last camera update action with the new one.

You can simply resolve that by creating more accurate camera update action (which would include both latlng change AND zoom level change):

CameraPosition newCamPos = new CameraPosition(new LatLng(13.0810,80.2740), 
                                                  15.5f, 
                                                  map.getCameraPosition().tilt, //use old tilt 
                                                  map.getCameraPosition().bearing); //use old bearing
map.animateCamera(CameraUpdateFactory.newCameraPosition(newCamPos), 4000, null);

ALTERNATIVELY as pointed out by MaciejGórski, you can just use newLatLngZoom interface which includes both LatLng and zoom change:

map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(13.0810,80.2740), 15.5f), 4000, null);
like image 186
Pavel Dudka Avatar answered Nov 18 '22 07:11

Pavel Dudka


Use CancelableCallback with first animateCamera and call second animateCamera in onFinish.

Example: AnimateCameraChainingExampleActivity.java

like image 38
MaciejGórski Avatar answered Nov 18 '22 07:11

MaciejGórski