How to adjust the zoom level to match the border, and then the central map in marker offset?

I have a google map (com.google.android.gms.maps.GoogleMap) where I have certain markers.

I can, separately,

1) adjust the scale and center the map on the border:

mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(getZoomBounds(), 10)); 

and

2) center the map over 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 is what I see on the map.

How to do it?

+6
source share
2 answers

Try using 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 this place, and animateCamera will provide a moving effect. They are linear in nature, so each can happen after the other, however, their separation, as I did above, will provide the potential effect that you are looking for.

If you are trying to see the actual movement of both calls in the user interface, you will need to register the completion of the animation for the callback message as necessary.

+1
source

For future visitors, you can associate a camera animation:

 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 of how the chain is infinite.

+3
source

Source: https://habr.com/ru/post/947570/


All Articles