Map

@available(iOS 13.0, *)
@_documentation(visibility: public)
@_spi(Experimental)
public struct Map : UIViewControllerRepresentable

A SwiftUI view that displays Mapbox Map.

Use Map do display Mapbox Map in SwiftUI application.

struct ContentView: View {
    static let polygon = Polygon(...)

    // Configures map camera to overview the given polygon.
    @State var viewport = Viewport.overview(geometry: Self.polygon)

    var body: some View {
        Map(viewport: $viewport) {
            // Displays user location.
            Puck2D(heading: bearing)

            // Displays view annotation.
            ViewAnnotation(CLLocationCoordinate(...))
                Text("🚀")
                    .background(Circle().fill(.red))
            }

            // Displays polygon annotation.
            PolygonAnnotation(polygon: Self.polygon)
                .fillColor(StyleColor(.systemBlue))
                .fillOpacity(0.5)
                .fillOutlineColor(StyleColor(.black))
                .onTapGesture {
                    print("Polygon is tapped")
                }
         }
         // Configures Mapbox Standard style to use "Dusk" preset.
         .mapStyle(.standard(lightPreset: .dusk))
    }
}
  • Creates a map that displays annotations.

    Declaration

    Swift

    @available(iOSApplicationExtension, unavailable)
    @_documentation(visibility: public)
    public init(
        viewport: Binding<Viewport>,
        @MapContentBuilder content: @escaping () -> MapContent
    )

    Parameters

    viewport

    The camera viewport to display.

    content

    A map content building closure.

  • Creates a map that displays annotations.

    Declaration

    Swift

    @available(iOSApplicationExtension, unavailable)
    @_documentation(visibility: public)
    public init(
        initialViewport: Viewport = .styleDefault,
        @MapContentBuilder content: @escaping () -> MapContent
    )

    Parameters

    initialViewport

    The camera viewport to display.

    content

    A map content building closure.

  • Declaration

    Swift

    public func makeCoordinator() -> Coordinator
  • Declaration

    Swift

    public func makeUIViewController(context: Context) -> UIViewController
  • Declaration

    Swift

    public func updateUIViewController(_ mapController: UIViewController, context: Context)
  • Adds an action to perform when the map is loaded.

    Declaration

    Swift

    @_documentation(visibility: public)
    func onMapLoaded(action: @escaping (MapLoaded) -> Void) -> Map
  • Adds an action to perform when there is an error occurred while loading the map.

    Declaration

    Swift

    @_documentation(visibility: public)
    func onMapLoadingError(action: @escaping (MapLoadingError) -> Void) -> Map
  • Adds an action to perform when the requested style is fully loaded.

    Declaration

    Swift

    @_documentation(visibility: public)
    func onStyleLoaded(action: @escaping (StyleLoaded) -> Void) -> Map
  • Adds an action to perform when the requested style data is loaded.

    Declaration

    Swift

    @_documentation(visibility: public)
    func onStyleDataLoaded(action: @escaping (StyleDataLoaded) -> Void) -> Map
  • Adds an action to perform when the camera is changed.

    Declaration

    Swift

    @_documentation(visibility: public)
    func onCameraChanged(action: @escaping (CameraChanged) -> Void) -> Map
  • Adds an action to perform when the map has entered the idle state.

    Declaration

    Swift

    @_documentation(visibility: public)
    func onMapIdle(action: @escaping (MapIdle) -> Void) -> Map
  • Adds an action to perform when a source is added.

    Declaration

    Swift

    @_documentation(visibility: public)
    func onSourceAdded(action: @escaping (SourceAdded) -> Void) -> Map
  • Adds an action to perform when a source is removed.

    Declaration

    Swift

    @_documentation(visibility: public)
    func onSourceRemoved(action: @escaping (SourceRemoved) -> Void) -> Map
  • Adds an action to perform when the source data is loaded.

    Declaration

    Swift

    @_documentation(visibility: public)
    func onSourceDataLoaded(action: @escaping (SourceDataLoaded) -> Void) -> Map
  • Adds an action to perform when the style image is missing.

    Declaration

    Swift

    @_documentation(visibility: public)
    func onStyleImageMissing(action: @escaping (StyleImageMissing) -> Void) -> Map
  • An image added to the style is no longer needed and can be removed using removeImage(withId:).

    Declaration

    Swift

    @_documentation(visibility: public)
    func onStyleImageRemoveUnused(action: @escaping (StyleImageRemoveUnused) -> Void) -> Map
  • Adds an action to perform when the map started rendering a frame.

    Declaration

    Swift

    @_documentation(visibility: public)
    func onRenderFrameStarted(action: @escaping (RenderFrameStarted) -> Void) -> Map
  • Adds an action to perform when the map finished rendering a frame.

    Declaration

    Swift

    @_documentation(visibility: public)
    func onRenderFrameFinished(action: @escaping (RenderFrameFinished) -> Void) -> Map
  • Adds an action to perform when a resource request is performed.

    Declaration

    Swift

    @_documentation(visibility: public)
    func onResourceRequest(action: @escaping (ResourceRequest) -> Void) -> Map
  • Adds a tap gesture handler to the map.

    The given action will be executed when other map gestures (such as quick zoom) failed, and no annotation or layer have handled the tap.

    Prefer to use this method instead of onTapGesture.

    Declaration

    Swift

    @_documentation(visibility: public)
    func onMapTapGesture(perform action: @escaping (MapContentGestureContext) -> Void) -> Map
  • Adds a long press gesture handler to the map.

    The given action will be executed when no annotation or layer have handled the long press.

    Declaration

    Swift

    @_documentation(visibility: public)
    func onMapLongPressGesture(perform action: @escaping (MapContentGestureContext) -> Void) -> Map
  • Adds a tap action to the specified layer.

    The handler will be called according to the order of visibility of layers at tap location.

    Declaration

    Swift

    @_documentation(visibility: public)
    func onLayerTapGesture(_ layerId: String, perform action: @escaping MapLayerGestureHandler) -> Map
  • Adds a long-press action to the specified layer.

    The handler will be called according to the order of visibility of layers at tap location.

    Declaration

    Swift

    @_documentation(visibility: public)
    func onLayerLongPressGesture(_ layerId: String, perform action: @escaping MapLayerGestureHandler) -> Map
  • Creates a map.

    Declaration

    Swift

    @available(iOSApplicationExtension, unavailable)
    @_documentation(visibility: public)
    public init(
        viewport: Binding<Viewport>
    )

    Parameters

    viewport

    The camera viewport to display.

  • Creates a map.

    Declaration

    Swift

    @available(iOSApplicationExtension, unavailable)
    @_documentation(visibility: public)
    public init(
        initialViewport: Viewport = .styleDefault
    )

    Parameters

    initialViewport

    Initial camera viewport.

  • Creates a map.

    Use this method to create a map in application extension context, or to override default url opening mechanism on iOS < 15.

    Note

    Starting from iOS 14 Map will use standard OpenURLAction taken from the Environment to open attribution urls, if urlOpener is not set.

    Declaration

    Swift

    @_documentation(visibility: public)
    public init(
        viewport: Binding<Viewport>,
        urlOpener: @escaping MapURLOpener,
        @MapContentBuilder content: @escaping () -> MapContent
    )

    Parameters

    viewport

    The camera viewport to display.

    urlOpener

    A closure that handles attribution url opening.

    content

    A map content building closure.

  • Creates a map.

    Use this method to create a map in application extension context, or to override default url opening mechanism on iOS < 15.

    Note

    Starting from iOS 14 Map will use standard OpenURLAction taken from the Environment to open attribution urls, if urlOpener is not set.

    Declaration

    Swift

    @_documentation(visibility: public)
    public init(
        initialViewport: Viewport = .styleDefault,
        urlOpener: @escaping MapURLOpener,
        @MapContentBuilder content: @escaping () -> MapContent
    )

    Parameters

    initialViewport

    The camera viewport to display.

    urlOpener

    A closure that handles attribution url opening.

    content

    A map content building closure.

  • Sets camera bounds.

    Declaration

    Swift

    @_documentation(visibility: public)
    func cameraBounds(_ cameraBounds: CameraBoundsOptions) -> Map
  • Sets style to the map.

    Declaration

    Swift

    @_documentation(visibility: public)
    func mapStyle(_ mapStyle: MapStyle) -> Map

    Parameters

    mapStyle

    A map style configuration.

  • Configures gesture options.

    Declaration

    Swift

    @_documentation(visibility: public)
    func gestureOptions(_ options: GestureOptions) -> Map
  • Sets constraint mode to the map. If not set, heightOnly will be in use.

    Declaration

    Swift

    @_documentation(visibility: public)
    func constrainMode(_ constrainMode: ConstrainMode) -> Map
  • Sets viewport mode to the map.

    Declaration

    Swift

    @_documentation(visibility: public)
    func viewportMode(_ viewportMode: ViewportMode) -> Map
  • Sets NorthOrientation to the map. If not set, upwards will be in use.

    Declaration

    Swift

    @_documentation(visibility: public)
    func northOrientation(_ northOrientation: NorthOrientation) -> Map
  • Sets OrnamentOptions to the map.

    Declaration

    Swift

    @_documentation(visibility: public)
    func ornamentOptions(_ options: OrnamentOptions) -> Map
  • Sets MapViewDebugOptions to the map.

    Declaration

    Swift

    @_documentation(visibility: public)
    func debugOptions(_ debugOptions: MapViewDebugOptions) -> Map
  • Map Coordinator.

    Declaration

    Swift

    public final class Coordinator