The camera

Contents

The camera is how the user views the map. It can be centered on a location, zoomed, tilted, rotated and moved. The camera can be set programmatically but also controlled through user interaction with gestures.

The camera is typically set using the CameraPosition class which is made up of a target, zoom, bearing and tilt parameters.

Attribute Type Description Default Setting
Target
LatLng object
Where the camera should center on the map. Required
Zoom
double
The zoom level of the camera; ranges from 0 to 22. 0
Bearing
double
The rotation of the camera in degrees clockwise from north. 0 (true north)
Tilt
double
The camera’s angle when viewing the map. Ranges from 0 to 60. 0 (bird’s eye view)

Setting the CameraPosition

There are many ways to set the camera position on the map, but the most common method is to use the CameraPosition’s Builder. An example:

CameraPosition position = new CameraPosition.Builder()
       .target(new LatLng(40.7584766,-73.9840227))
       .zoom(13)
       .build();
map.setCameraPosition(position);

The above code sets the camera’s target/center to be a coordinate in New York City and sets the zoom level to 13. Once the CameraPosition is built, the map is updated with the new position.

Animating the CameraPosition

In addition to setting the map’s camera position, you can also animate a “flight” for the new camera position information, gliding into it. The map’s animateCamera() method allows for this using a CameraUpdate object as a parameter. An example of this is below:

CameraPosition position = new CameraPosition.Builder()
       .target(new LatLng(40.7584766,-73.9840227))
       .zoom(13)
       .build();

map.animateCamera(CameraUpdateFactory.newCameraPosition(position), 1000);

Similar to the previous example, the CameraPosition is created, [CameraUpdateFactory]TK can then use this position information to generate the CameraUpdate object animateCamera() requires. The second parameter is an optional duration for the animation in milliseconds.

Last updated September 22, 2021.