Skip to main content

Map recorder

This example demonstrates the MapRecorder feature in the Mapbox Maps SDK for iOS. In this implementation, when the map's style is loaded, a MapRecorder instance is created using mapView.mapboxMap.makeRecorder() and the recording is started. A camera animation is then initiated, flying to a specific location defined by CameraOptions. After the camera animation completes, the map recording is stopped, and the recorded sequence is replayed twice at double speed using the replay method with MapPlayerOptions to customize playback settings. Additionally, the playbackState method is called to print the playback state of the recorder.

iOS Examples App Available

This example code is part of the Maps SDK for iOS Examples App, a working iOS project available on Github. iOS developers are encouraged to run the examples app locally to interact with this example in an emulator and explore other features of the Maps SDK.

See our Run the Maps SDK for iOS Examples App tutorial for step-by-step instructions.

MapRecorderExample.swift
import UIKit
@_spi(Experimental) import MapboxMaps

final class ViewController: UIViewController {

private var mapView: MapView!
private var cancelables = Set<AnyCancelable>()

override func viewDidLoad() {
super.viewDidLoad()

mapView = MapView(frame: view.bounds)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
mapView.ornaments.options.scaleBar.visibility = .visible

view.addSubview(mapView)

mapView.mapboxMap.onStyleLoaded.observeNext { _ in
// Once the Style is loaded, create the ``MapRecorder`` and start the recording
guard let recorder = try? self.mapView.mapboxMap.makeRecorder() else { return }
recorder.start()

// Build a new set of CameraOptions for the map to fly to
let cameraOptions = CameraOptions(center: CLLocationCoordinate2D(latitude: 45.4588, longitude: -73.581), zoom: 11, pitch: 35)

self.mapView.camera.fly(to: cameraOptions, duration: 10, completion: { _ in
// When the camera animation is complete, stop the map recording
// Replay the camera animation twice at double speed by passing the recorded sequence returned from the stop method
let mapRecordingSequence = recorder.stop()
recorder.replay(recordedSequence: mapRecordingSequence, options: MapPlayerOptions(playbackCount: 2, playbackSpeedMultiplier: 2.0, avoidPlaybackPauses: false)) {
print(recorder.playbackState())
}
})
}.store(in: &cancelables)
}


}
Was this example helpful?