Camera and animations
The camera position controls how the map is displayed. The camera position is a combination of its location, zoom, tilt, and rotation. To change the user’s view of the map, change the camera position. The map in the Map Display Compose module is represented as a globe, so that the sizes and shapes of the features are not distorted.
The default position of the camera is:
- Located at 0 latitude and 0 longitude.
- With a zoom of 0, which shows the whole globe.
- Rotated to be north-up.
- Tilted to point straight down.
Changing the camera position does not affect the map style, markers or any other map configuration.
Camera state
The CameraState represents the entry point for managing the camera position and animations. It provides functionality to move or animate the camera, track its current state, and zoom to specific areas or markers. The CameraState can be accessed via the MapViewState, which is created during the initialization of the map. This design ensures a clear and structured way to interact with the camera functionality.
val cameraState = mapViewState.cameraState
Camera options
In the Map Display Compose module, every change to the camera’s position has to be done using the CameraOptions class. It specifies the camera’s location, zoom, tilt, and rotation to set.
Location
The location is the geographical coordinate marking where the camera view should be pointed. The map view is always centered on the location.
The position is specified with a GeoPoint object.
Zoom
The camera zoom can be set between 0 and 22. Approximate zoom scales:
- 0 - The camera is fully zoomed out the whole map is visible.
- 5 - Country-level.
- 10 - City-level.
- 15 - Neighborhood-level.
- 20 - Street-level.
- 22 - Maximum zoom in.
Tilt
The tilt is the angle from the nadir (the camera pointing toward the center of the earth), where the tilt is set to zero 0. When the tilt is 90, the camera is pointing towards the horizon.
The closer the tilt level approaches 90, the more tiles may need to be downloaded. This can result in high data usage.
Rotation
Sets the cardinal direction that the camera is heading. By default, the rotation is set to 0, meaning that the orientation of the map is toward the north. The direction increases as the camera rotates clockwise. This means that east is 90 degrees, south is 180 degrees, and west is 270.
Current camera position
You can access the current camera position by reading CameraState.data.position in the MapViewState instance. CameraState.data returns null when map is not ready yet. The CameraPosition object that is returned contains a snapshot of the camera’s location, zoom, tilt, and rotation.
val cameraPosition = mapViewState.cameraState.data?.position
Moving the camera
The Map Display Compose module provides two ways of changing the camera position: transitioning with a smooth animation or moving immediately to the new position.
To change the camera position immediately, call the moveCamera method on the MapViewState.cameraState object.
1val amsterdam = GeoPoint(52.379189, 4.899431)2val cameraOptions =3 CameraOptions(4 position = amsterdam,5 zoom = 10.0,6 tilt = 45.0,7 rotation = 90.0,8 )9mapViewState.cameraState.moveCamera(cameraOptions)
Animating the camera move
To animate a camera move, first define CameraOptions with the new camera position. Then use the animateCamera function to set the duration of the animation. The animateCamera function is a suspend function, meaning it must be called within a coroutine. The animation time is set to 1.5 seconds by default.
Note that setting duration to
nullis equivalent to using themoveCameramethod.
1LaunchedEffect(mapViewState) {2 val amsterdam = GeoPoint(52.379189, 4.899431)3 val cameraOptions = CameraOptions(4 position = amsterdam,5 zoom = 10.0,6 tilt = 45.0,7 rotation = 90.0,8 )9 mapViewState.cameraState.animateCamera(cameraOptions, 3.seconds)10}
Tracking mode
The Map Display Compose module allows you to set how the camera tracks the user locations to suit different interaction modes. Do this by setting CameraTrackingMode to the MapViewState.cameraState instance. There are five options for tracking mode:
None- The camera does not track the user’s location. This is the default setting and it is used primarily to show the user’s location on the map.FollowNorthUp- The camera follows the user’s location, but its tilt and zoom do not change.FollowDirection- The camera follows the user’s location and heading to position the camera in the heading direction. Camera is always following from the top (the tilt is set to 0 degrees).FollowRouteDirection- The camera follows the user’s location and heading to best present the route. Camera properties like tilt and zoom may be adjusted to better display the route and its guidance instructions.FollowRouteNorthUp- The camera follows the user’s location; the camera is fixed to due north. Camera properties like tilt and zoom may be adjusted to better display the route and its guidance instructions.RouteOverview- The camera tries to fit the routes in the current view, by changing the zoom level and other camera properties.
The last three modes are mainly used in navigation applications.
To apply a given tracking mode, set CameraState.trackingMode:
mapViewState.cameraState.trackingMode = CameraTrackingMode.FollowRouteDirection
To check what tracking mode is currently set:
val cameraTrackingMode = mapViewState.cameraState.trackingMode
Next steps
Since you have learned how to manipulate camera positions, here are the recommended next steps: