CameraUpdateFactory

  • TheCameraUpdateFactory class provides methods to createCameraUpdate objects for modifying a map's camera.

  • To change a map's camera, use theCameraUpdate object created by this class with methods likeGoogleMap.animateCamera orGoogleMap.moveCamera.

  • This class must be initialized by waiting for aGoogleMap to be available from a map fragment/view or by callingMapsInitializer.initialize.

  • TheCameraUpdateFactory offers various methods to control the camera, including setting position and zoom, fitting bounds, and scrolling.

public final classCameraUpdateFactory extendsObject

A class containing methods for creatingCameraUpdate objects that change a map's camera. To modify the map's camera, call GoogleMap.animateCamera(CameraUpdate), GoogleMap.animateCamera(CameraUpdate, GoogleMap.CancelableCallback) or GoogleMap.moveCamera(CameraUpdate), using aCameraUpdate object created with this class.

For example, to zoom in on a map, you can use the following code:

GoogleMap map = ...; map.animateCamera(CameraUpdateFactory.zoomIn());

Prior to using any methods from this class, you must do one of the following to ensure that this class is initialized:

Developer Guide

For more information, read theCamera and View developer guide.

Public Method Summary

staticCameraUpdate
newCameraPosition(CameraPosition cameraPosition)
Returns aCameraUpdate that moves the camera to a specifiedCameraPosition.
staticCameraUpdate
newLatLng(LatLng latLng)
Returns aCameraUpdate that moves the center of the screen to a latitude and longitude specified by aLatLng object.
staticCameraUpdate
newLatLngBounds(LatLngBounds bounds, int padding)
Returns aCameraUpdate that transforms the camera such that the specified latitude/longitude bounds are centered on screen at the greatest possible zoom level.
staticCameraUpdate
newLatLngBounds(LatLngBounds bounds, int width, int height, int padding)
Returns aCameraUpdate that transforms the camera such that the specified latitude/longitude bounds are centered on screen within a bounding box of specified dimensions at the greatest possible zoom level.
staticCameraUpdate
newLatLngZoom(LatLng latLng, float zoom)
Returns aCameraUpdate that moves the center of the screen to a latitude and longitude specified by aLatLng object, and moves to the given zoom level.
staticCameraUpdate
scrollBy(float xPixel, float yPixel)
Returns aCameraUpdate that scrolls the camera over the map, shifting the center of view by the specified number of pixels in the x and y directions.
staticCameraUpdate
zoomBy(float amount,Point focus)
Returns aCameraUpdate that shifts the zoom level of the current camera viewpoint.
staticCameraUpdate
zoomBy(float amount)
Returns aCameraUpdate that shifts the zoom level of the current camera viewpoint.
staticCameraUpdate
zoomIn()
Returns aCameraUpdate that zooms in on the map by moving the viewpoint's height closer to the Earth's surface.
staticCameraUpdate
zoomOut()
Returns aCameraUpdate that zooms out on the map by moving the viewpoint's height farther away from the Earth's surface.
staticCameraUpdate
zoomTo(float zoom)
Returns aCameraUpdate that moves the camera viewpoint to a particular zoom level.

Inherited Method Summary

From class java.lang.Object
Object
clone()
boolean
equals(Object arg0)
void
finalize()
finalClass<?>
getClass()
int
hashCode()
final void
notify()
final void
notifyAll()
String
toString()
final void
wait(long arg0, int arg1)
final void
wait(long arg0)
final void
wait()

Public Methods

public staticCameraUpdatenewCameraPosition(CameraPosition cameraPosition)

Returns aCameraUpdate that moves the camera to a specifiedCameraPosition. In effect, this creates a transformation from theCameraPosition object's latitude, longitude, zoom level, bearing and tilt.

Parameters
cameraPositionThe requested position. Must not benull.
Returns

public staticCameraUpdatenewLatLng(LatLng latLng)

Returns aCameraUpdate that moves the center of the screen to a latitude and longitude specified by aLatLng object. This centers the camera on theLatLng object.

Parameters
latLngaLatLng object containing the desired latitude and longitude. Must not benull.
Returns

public staticCameraUpdatenewLatLngBounds(LatLngBounds bounds, int padding)

Returns aCameraUpdate that transforms the camera such that the specified latitude/longitude bounds are centered on screen at the greatest possible zoom level. You can specify padding, in order to inset the bounding box from the map view's edges. The returnedCameraUpdate has a bearing of 0 and a tilt of 0.

Do not change the camera with this camera update until the map has undergone layout (in order for this method to correctly determine the appropriate bounding box and zoom level, the map must have a size). Otherwise anIllegalStateException will be thrown. It is NOT sufficient for the map to be available; the view containing the map must have also undergone layout such that its dimensions have been determined. If you cannot be sure that this has occurred, use newLatLngBounds(LatLngBounds, int, int, int) instead and provide the dimensions of the map manually.

Parameters
boundsregion to fit on screen. Must not benull.
paddingspace (in px) to leave between the bounding box edges and the view edges. This value is applied to all four sides of the bounding box.
Returns

public staticCameraUpdatenewLatLngBounds(LatLngBounds bounds, int width, int height, int padding)

Returns aCameraUpdate that transforms the camera such that the specified latitude/longitude bounds are centered on screen within a bounding box of specified dimensions at the greatest possible zoom level. You can specify additional padding, to further restrict the size of the bounding box. The returnedCameraUpdate has a bearing of 0 and a tilt of 0.

Unlike newLatLngBounds(LatLngBounds, int), you can use theCameraUpdate returned by this method to change the camera prior to the map's the layout phase, because the arguments specify the desired size of the bounding box.

Parameters
boundsthe region to fit in the bounding box. Must not benull.
widthbounding box width in pixels (px)
heightbounding box height in pixels (px)
paddingadditional size restriction (in px) of the bounding box
Returns

public staticCameraUpdatenewLatLngZoom(LatLng latLng, float zoom)

Returns aCameraUpdate that moves the center of the screen to a latitude and longitude specified by aLatLng object, and moves to the given zoom level.

Parameters
latLngaLatLng object containing the desired latitude and longitude. Must not benull.
zoomthe desired zoom level, in the range of 2.0 to 21.0. Values below this range are set to 2.0, and values above it are set to 21.0. Increase the value to zoom in. Not all areas have tiles at the largest zoom levels.
Returns

public staticCameraUpdatescrollBy(float xPixel, float yPixel)

Returns aCameraUpdate that scrolls the camera over the map, shifting the center of view by the specified number of pixels in the x and y directions.

Examples:

  • If xPixel = 5 and yPixel = 0, the system scrolls right by moving the camera so that the map appears to have shifted 5 pixels to the left.
  • If xPixel = 0 and yPixel = 5, the system scrolls down by moving the camera so that the map appears to have shifted 5 pixels upwards.

The scrolling is relative to the camera's current orientation. For example, if the camera is bearing 90 degrees, then east is "up" and scrolling right will move the camera south.

Parameters
xPixelthe number of pixels to scroll horizontally. A positive value moves the camera to the right, with respect to its current orientation. A negative value moves the camera to the left, with respect to its current orientation.
yPixelthe number of pixels to scroll vertically. A positive value moves the camera downwards, with respect to its current orientation. A negative value moves the camera upwards, with respect to its current orientation.
Returns

public staticCameraUpdatezoomBy(float amount,Point focus)

Returns aCameraUpdate that shifts the zoom level of the current camera viewpoint.

A point specified by focus will remain fixed (i.e., it corresponds to the same lat/long both before and after the zoom process).

This method is different to zoomTo(float) in that zoom is relative to the current camera.

For example, if theLatLng (11.11, 22.22) is currently at the screen location (23, 45). After calling this method with a zoom amount and thisLatLng, the screen location of thisLatLng will still be (23, 45).

Parameters
amountamount to change the zoom level. Positive values indicate zooming closer to the surface of the Earth while negative values indicate zooming away from the surface of the Earth.
focuspixel location on the screen that is to remain fixed after the zooming process. The lat/long that was at that pixel location before the camera move will remain the same after the camera has moved. Must not benull.
Returns

public staticCameraUpdatezoomBy(float amount)

Returns aCameraUpdate that shifts the zoom level of the current camera viewpoint.

This method is different to zoomTo(float) in that zoom is relative to the current camera.

Parameters
amountamount to change the zoom level. Positive values indicate zooming closer to the surface of the Earth while negative values indicate zooming away from the surface of the Earth.
Returns

public staticCameraUpdatezoomIn()

Returns aCameraUpdate that zooms in on the map by moving the viewpoint's height closer to the Earth's surface. The zoom increment is 1.0.

Returns

public staticCameraUpdatezoomOut()

Returns aCameraUpdate that zooms out on the map by moving the viewpoint's height farther away from the Earth's surface. The zoom increment is -1.0.

Returns

public staticCameraUpdatezoomTo(float zoom)

Returns aCameraUpdate that moves the camera viewpoint to a particular zoom level.

Parameters
zoomthe desired zoom level, in the range of 2.0 to 21.0. Values below this range are set to 2.0, and values above it are set to 21.0. Increase the value to zoom in. Not all areas have tiles at the largest zoom levels.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2024-10-31 UTC.