Camera Controls

To create rich experiences on top of the Mappedin iOS SDK, it's useful to be able to control the map view programmatically. This guide shows how to focus the map view on targets, how to listen to camera events and how to control the camera.

Focus the Camera on Targets

To focus on a polygon after it has been touched, we use the onPolygonClicked() function in the MPIMapViewDelegate. In this case, we are targeting only the touched polygon. However, we can also give coordinates or nodes as MPIOptions.CameraTargets.

func onPolygonClicked(polygon: MPIPolygon) {
mapView?.cameraManager.focusOn(targets: MPIOptions.CameraTargets(polygons: [polygon]))

Listening to Camera Events

We can use the onCameraChanged() function in the MPIMapViewDelegate to monitor changes to the camera position, rotation, tilt, and zoom.

func onCameraChanged(cameraChange: Mappedin.MPICameraTransform) {
print("Position \(cameraChange.position)")
print("Rotation \(cameraChange.rotation)")
print("Tilt \(cameraChange.tilt)")
print("Zoom \(cameraChange.zoom)")

Controlling the Camera

Controlling the camera to set it to a specific tilt, rotation or zoom can be done with mapView.cameraManager.set() and configured MPIOptions.CameraConfiguration(). Tilt and rotation are set as radians, where as zoom is in the camera distance in meters from the target.

cameraTransform: MPIOptions.CameraConfiguration(
zoom: 1234,
tilt: 0.3,
rotation: 1.5

It is possible to limit the tilting of the camera by listening to the change event and setting it to a desired value in onCameraChanged().

func onCameraChanged(cameraChange: Mappedin.MPICameraTransform) {
let tilt = 0.0
if(cameraChange.tilt != tilt) {
mapView?.cameraManager.set(cameraTransform: MPIOptions.CameraConfiguration(tilt: tilt))

If you want to be able to zoom in closer than the default limit, use the setter mapView?.cameraManager.setMinZoom(zoomLevel: 200.0).

Was this page helpful?

Next Topic

Blue Dot