Map

@available(iOS 13.0, *)
 @_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)
    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)
    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

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

    Declaration

    Swift

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

    Declaration

    Swift

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

    Declaration

    Swift

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

    Declaration

    Swift

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

    Declaration

    Swift

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

    Declaration

    Swift

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

    Declaration

    Swift

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

    Declaration

    Swift

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

    Declaration

    Swift

     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

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

    Declaration

    Swift

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

    Declaration

    Swift

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

    Declaration

    Swift

     func onResourceRequest(action: @escaping (ResourceRequest) -> Void) -> Map
  • Creates a map.

    Declaration

    Swift

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

    Parameters

    viewport

    The camera viewport to display.

  • Creates a map.

    Declaration

    Swift

    @available(iOSApplicationExtension, unavailable)
    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

    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

    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

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

    Declaration

    Swift

     func mapStyle(_ mapStyle: MapStyle) -> Map

    Parameters

    mapStyle

    A map style configuration.

  • Configures gesture options.

    Declaration

    Swift

     func gestureOptions(_ options: GestureOptions) -> Map
  • Adds tap handler to the map.

    Prefer using this handler instead of onTapGesture since it waits for the failure of other map gestures like quick-zoom.

    Declaration

    Swift

     func onMapTapGesture(perform action: @escaping MapTapAction) -> Map
  • Adds tap action to layers with specified layerIds.

    The action will only be called when at least one of specified layers are in the tap viewport.

    Declaration

    Swift

     func onLayerTapGesture(_ layerIds: String..., perform action: @escaping MapLayerTapAction) -> Map
  • Sets constraint mode to the map. If not set, heightOnly will be in use.

    Declaration

    Swift

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

    Declaration

    Swift

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

    Declaration

    Swift

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

    Declaration

    Swift

     func ornamentOptions(_ options: OrnamentOptions) -> Map
  • Map Coordinator.

    Declaration

    Swift

    public final class Coordinator