Reputation: 1818
I have a google map (com.google.android.gms.maps.GoogleMap) where I have some markers set.
I am able to, separately,
1) adjust zoom level and center the map on a boundary:
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(getZoomBounds(), 10));
and
2) center the map above one of the markers:
LatLng poiSelectedLatLng = new LatLng(markerSelected.getPosition().latitude
+ offset, markerSelected.getPosition().longitude);
mMap.animateCamera(CameraUpdateFactory.newLatLng(poiSelectedLatLng));
but, for the life of me, I can't just do both, adjust the zoom level using newLatLngBounds and then center the map somewhere else. Whatever I do last is what I see happening in the map.
How do I do this?
Upvotes: 7
Views: 6736
Reputation: 1386
Made this extension functions to conveniently chain multiple camera updates.
fun GoogleMap.animateCameraWith(vararg cameraUpdates: CameraUpdate) {
var i = 0
animateCamera(cameraUpdates[i++], object: GoogleMap.CancelableCallback {
override fun onCancel() {}
override fun onFinish() {
if (i < cameraUpdates.size) {
animateCamera(cameraUpdates[i++], this)
}
}
})
}
Taking the case of OP, it will be as simple as calling
mMap.animateCameraWith(
CameraUpdateFactory.newLatLngBounds(getZoomBounds(), 10),
CameraUpdateFactory.newLatLng(poiSelectedLatLng),
// Add as many as you want
)
Upvotes: 0
Reputation: 22232
For future visitors this is how you can chain camera animations:
map.animateCamera(CameraUpdateFactory.newLatLngBounds(getZoomBounds(), 10), 2000, new CancelableCallback() {
@Override
public void onFinish() {
LatLng poiSelectedLatLng = new LatLng(markerSelected.getPosition().latitude + offset, markerSelected.getPosition().longitude);
map.animateCamera(CameraUpdateFactory.newLatLng(poiSelectedLatLng));
}
@Override
public void onCancel() {
}
});
Also see AnimateCameraChainingExampleActivity.java for an example how to chain infinitely.
Upvotes: 6
Reputation: 24723
Try using both moveCamera
and animateCamera
...
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(getZoomBounds(), 10));
LatLng poiSelectedLatLng = new LatLng(markerSelected.getPosition().latitude
+ offset, markerSelected.getPosition().longitude);
mMap.animateCamera(CameraUpdateFactory.newLatLng(poiSelectedLatLng));
moveCamera
will move directly to that spot while animateCamera
will provide the moving effect. They are linear in nature so one will happen after the other however layering them as I have done above will provide the potential effect you are looking for.
If you are trying to see the actual movement of both calls on the UI you will need to register for the callback post the completion of the animation as needed.
Upvotes: 2