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
|Where the camera should center on the map.||Required|
|The zoom level of the camera; ranges from 0 to 22.||0|
|The rotation of the camera in degrees clockwise from north.||0 (true north)|
|The camera’s angle when viewing the map. Ranges from 0 to 60.||0 (bird’s eye view)|
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.
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
animateCamera() requires. The second parameter is an optional duration for the animation in milliseconds.