MGLMapSnapshotter
@interface MGLMapSnapshotter : NSObject
An MGLMapSnapshotter
generates static raster images of the map. Each snapshot
image depicts a portion of a map defined by an MGLMapSnapshotOptions
object
you provide. The snapshotter generates an MGLMapSnapshot
object
asynchronously, passing it into a completion handler once tiles and other
resources needed for the snapshot are finished loading.
You can change the snapshotter’s options at any time and reuse the snapshotter for multiple distinct snapshots; however, the snapshotter can only generate one snapshot at a time. If you need to generate multiple snapshots concurrently, create multiple snapshotter objects.
For an interactive map, use the MGLMapView
class. Both MGLMapSnapshotter
and MGLMapView
are compatible with offline packs managed by the
MGLOfflineStorage
class.
From a snapshot, you can obtain an image and convert geographic coordinates to
the image’s coordinate space in order to superimpose markers and overlays. If
you do not need offline map functionality, you can use the Snapshot
class in
MapboxStatic.swift to generate
static map images with overlays.
Example
let camera = MGLMapCamera(lookingAtCenter: CLLocationCoordinate2D(latitude: 37.7184, longitude: -122.4365), altitude: 100, pitch: 20, heading: 0)
let options = MGLMapSnapshotOptions(styleURL: MGLStyle.satelliteStreetsStyleURL, camera: camera, size: CGSize(width: 320, height: 480))
options.zoomLevel = 10
let snapshotter = MGLMapSnapshotter(options: options)
snapshotter.start { (snapshot, error) in
if let error = error {
fatalError(error.localizedDescription)
}
image = snapshot?.image
}
Related examples
See the
Create a static map snapshot example to learn how to use the
MGLMapSnapshotter
to generate a static image based on an MGLMapView
object’s style, camera, and view bounds.
-
Initializes and returns a map snapshotter object that produces snapshots according to the given options.
Declaration
Objective-C
- (nonnull instancetype)initWithOptions: (nonnull MGLMapSnapshotOptions *)options;
Swift
init(options: MGLMapSnapshotOptions)
Parameters
options
The options to use when generating a map snapshot.
Return Value
An initialized map snapshotter.
-
Starts the snapshot creation and executes the specified block with the result.
Declaration
Objective-C
- (void)startWithCompletionHandler: (nonnull MGLMapSnapshotCompletionHandler)completionHandler;
Swift
func start(completionHandler: @escaping MGLMapSnapshotCompletionHandler)
Parameters
completionHandler
The block to handle the result in.
-
Starts the snapshot creation and executes the specified block with the result on the specified queue.
Declaration
Objective-C
- (void)startWithQueue:(nonnull dispatch_queue_t)queue completionHandler: (nonnull MGLMapSnapshotCompletionHandler)completionHandler;
Swift
func start(with queue: DispatchQueue, completionHandler: @escaping MGLMapSnapshotCompletionHandler)
Parameters
queue
The queue to handle the result on.
completionHandler
The block to handle the result in.
-
Starts the snapshot creation and executes the specified blocks with the result on the specified queue. Use this option if you want to add custom drawing on top of the resulting
MGLMapSnapShot
.Declaration
Objective-C
- (void)startWithOverlayHandler: (nonnull MGLMapSnapshotOverlayHandler)overlayHandler completionHandler: (nonnull MGLMapSnapshotCompletionHandler)completionHandler;
Swift
func start(overlayHandler: @escaping MGLMapSnapshotOverlayHandler, completionHandler: @escaping MGLMapSnapshotCompletionHandler)
Parameters
overlayHandler
The block to handle manipulation of the
MGLMapSnapshotter
‘sCGContext
.completionHandler
The block to handle the result in.
-
Cancels the snapshot creation request, if any.
Once you call this method, you cannot resume the snapshot. In order to obtain the snapshot, create a new
MGLMapSnapshotter
object.Declaration
Objective-C
- (void)cancel;
Swift
func cancel()
-
The options to use when generating a map snapshot.
Declaration
Objective-C
@property (assign, readwrite, nonatomic) MGLMapSnapshotOptions *_Nonnull options;
Swift
var options: MGLMapSnapshotOptions { get set }
-
Indicates whether a snapshot is currently being generated.
Declaration
Objective-C
@property (readonly, getter=isLoading, nonatomic) BOOL loading;
Swift
var isLoading: Bool { get }