Camera and animations

VERSION 0.44.1
PUBLIC PREVIEW

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 SDK is represented as a globe so that the sizes and shapes of features are not distorted.

The default position of the camera is:

  • Located at 0 latitude and 0 longitude.
  • With a zoom of 1, 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 update

In the Map Display SDK, every change to the camera’s position must be done using the CameraUpdate class. The CameraUpdate class 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 it.

The position is specified with a CLLocationCoordinate2D object.

Zoom

The camera zoom can be set between 0 and 22. Approximate zoom scales:

  • 1 - The camera is fully zoomed out. The whole globe 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 calling the CameraActions.cameraProperties parameter on the TomTomMap instance. The CameraProperties object that is returned contains a snapshot of the camera’s location, zoom, tilt, and rotation.

let cameraProperties = map.cameraProperties

Moving the camera

The Map Display SDK provides two ways of changing the camera position: transitioning with a smooth animation, or moving immediately to the new position.

To immediately change the camera position, call the CameraActions.moveCamera(_:) method on the TomTomMap object.

1let amsterdam = CLLocationCoordinate2D(latitude: 52.379189, longitude: 4.899431)
2let cameraUpdate = CameraUpdate(
3 position: amsterdam,
4 zoom: 10.0,
5 tilt: 45.0,
6 rotation: 90.0
7)
8map.moveCamera(cameraUpdate)

Animating the camera move

To animate a camera move, first define CameraUpdate with the new camera position. You can then use the TimeInterval class to set the duration of the animation. The animation time is set to 1.5 seconds by default.

1let amsterdam = CLLocationCoordinate2D(latitude: 52.379189, longitude: 4.899431)
2let cameraUpdate = CameraUpdate(
3 position: amsterdam,
4 zoom: 10.0,
5 tilt: 45.0,
6 rotation: 90.0
7)
8map.applyCamera(cameraUpdate)

The animation can also be monitored so that you receive a completion block when it completes. You can cancel an animation by moving the camera before the animation is finished.

1let amsterdam = CLLocationCoordinate2D(latitude: 52.379189, longitude: 4.899431)
2let cameraUpdate = CameraUpdate(
3 position: amsterdam,
4 zoom: 10.0,
5 tilt: 45.0,
6 rotation: 90.0
7)
8map.applyCamera(cameraUpdate, animationDuration: 0.4) { success in
9 if success {
10 /* YOUR CODE GOES HERE */
11 }
12}

Tracking mode

The Map Display SDK allows you to set how the camera tracks the user’s locations to suit different interaction modes.

Do this by setting CameraTrackingMode to the TomTomMap instance. There are six options for tracking mode:

  • CameraTrackingMode.none - The camera does not track the user’s location. This is the default setting and is mainly used to show the user’s location on the map.
  • CameraTrackingMode.followNorthUp(_:) - The camera follows the user’s location, but its tilt and zoom do not change.
  • CameraTrackingMode.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.
  • CameraTrackingMode.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)
  • CameraTrackingMode.followRouteNorthUp(_:) - The camera centers the position marker within the safe area, fixing the camera’s heading north. The map remains unrotated, the marker’s orientation shifts, and the tilt is a top-down view at 0.
  • CameraTrackingMode.routeOverview - The camera attempts 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 the CameraTrackingMode parameter:

map.cameraTrackingMode = .followRouteDirection()

To check what tracking mode is currently set:

let cameraTrackingMode = map.cameraTrackingMode

Listening on camera events

The camera position can be modified by calling CameraActions.moveCamera(_:) and CameraActions.applyCamera(_:animationDuration:completion:). But users can also affect the camera when they use gestures to interact with the map. You can listen to MapDelegate methods.

In the first step you have to set the delegate:

map.delegate = self

Next, add the MapDelegate.map(_:onCameraEvent:) protocol method.

1func map(_ map: TomTomMap, onCameraEvent event: CameraEvent) {
2 switch event {
3 case let .trackingModeChanged(mode):
4 break
5 case let .cameraSteady(properties):
6 break
7 case let .cameraChanged(properties):
8 break
9 case let .cameraSignificantlyChanged(properties):
10 break
11 }
12}