I'd like to both zoom and scroll a GoogleMap
object at the same time. Unfortunately, it seems like if I just make two moveCamera
calls (one after another), only the second call appears to take effect.
An alternative approach would be to pass a CameraPosition
, but unfortunately it looks like the CameraPosition
constructor does not take an argument that deals with an amount to scroll (which is invariant to the zoom level), but only an argument as to what lat/lon to go to.
Is there some clever way to combine/concatenate CameraUpdate
objects so I can just issue one moveCamera
command that does both a pan and a zoom?
I assume something like this is possible because you can do it when you're touching the map. You put down two fingers, and you can zoom in/out by spreading your fingers and pan by moving them both simultaneously.
It looks like your best option will be to modify the CameraPosition
object returned when you call GoogleMap.getCameraPosition()
. However, you won't be able to just increment that latitude and longitude of the CameraPosition
object by the number of pixels you scrolled. You'll have to get the CameraPosition
's LatLng coordinates, convert that to a Point
object using the Projection
class, modify the Point
, and then convert it back into a LatLng
object.
For example:
GoogleMap map; // This is your GoogleMap object
int dx; // X distance scrolled
int dy; // Y distance scrolled
float dz; // Change in zoom level
float originalZoom;
CameraPosition position = map.getCameraPosition();
Project projection = map.getProjection();
LatLng mapTarget = position.target;
originalZoom = position.zoom;
Point mapPoint = projection.toScreenLocation(mapTarget);
mapPoint.x += dx;
mapPoint.y += dy;
mapTarget = projection.fromScreenLocation(mapPoint);
CameraPosition newPosition = new CameraPosition(position)
.target(mapTarget)
.zoom(originalZoom+dz)
.build();
map.moveCamera(CameraUpdateFactory.newCameraPosition(newPosition));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With