Viewport

@available(iOS 13.0, *)
@_documentation(visibility: public)
@_spi(Experimental)
public struct Viewport : Equatable

The viewport represents the ways to position camera.

The viewport may be set to the map as initial viewport:

 Map(initialViewport: .styleDefault)

or as binding:

struct UserLocationMap: View {
  @State var viewport: Viewport = .followPuck(zoom: 16, bearing: .heading)
  var body: some View {
    Map(viewport: $viewport)
  }
}

Viewport change can be animated via the withViewportAnimation(_:body:completion:).

  struct AnimatedMap: View {
      @State var viewport: Viewport = .styleDefault
      var body: some View {
          Map(viewport: $viewport)
              .overlay {
                  Button("Locate the user") {
                      withViewportAnimation {
                          viewport = .followPuck(zoom: 16, bearing: .heading, pitch: 60)
                      }
                  }
              }
      }
  }
  • Options for the overview viewport.

    See more

    Declaration

    Swift

    @_documentation(visibility: public)
    public struct OverviewOptions : Equatable
  • Options for the follow puck viewport.

    See more

    Declaration

    Swift

    @_documentation(visibility: public)
    public struct FollowPuckOptions : Equatable
  • Represent insets configuration.

    Inset configuration is applicable to every kind of viewport configuration except idle.

    Declaration

    Swift

    @_documentation(visibility: public)
    public struct InsetOptions : Equatable
  • Configures insets of viewport.

    Declaration

    Swift

    @_documentation(visibility: public)
    public var insetOptions: Viewport.InsetOptions
  • Idle viewport represents the state when user freely drags the map.

    The viewport is automatically switches to idle state when the user starts dragging the map. Setting the idle viewport results in cancelling any ongoing camera animation.

    Declaration

    Swift

    @_documentation(visibility: public)
    public static var idle: Viewport { get }
  • Sets camera to the default camera options defined in the current style.

    See more in the Mapbox Style Specification.

    Declaration

    Swift

    @_documentation(visibility: public)
    public static var styleDefault: Viewport { get }
  • Manually sets camera to specified properties.

    Declaration

    Swift

    @_documentation(visibility: public)
    public static func camera(center: CLLocationCoordinate2D? = nil,
                              anchor: CGPoint? = nil,
                              zoom: CGFloat? = nil,
                              bearing: CLLocationDirection? = nil,
                              pitch: CGFloat? = nil) -> Viewport

    Parameters

    center

    The geographic coordinate that will be rendered at the midpoint of the map.

    anchor

    Point in the map’s coordinate system about which zoom and bearing should be applied. Mutually exclusive with center.

    zoom

    The zoom level of the map.

    bearing

    The bearing of the map, measured in degrees clockwise from true north.

    pitch

    Pitch toward the horizon measured in degrees, with 0 degrees resulting in a top-down view for a two-dimensional map.

    Return Value

    A viewport configured with given camera settings.

  • Configures camera to show overview of the specified geometry.

    Declaration

    Swift

    @_documentation(visibility: public)
    public static func overview(
        geometry: GeometryConvertible,
        bearing: CGFloat = 0,
        pitch: CGFloat = 0
    ) -> Viewport

    Parameters

    geometry

    Geometry to show.

    bearing

    The bearing of the map, measured in degrees clockwise from true north.

    pitch

    Pitch toward the horizon measured in degrees, with 0 degrees resulting in a top-down view for a two-dimensional map.

    Return Value

    A viewport configured with given overview settings.

  • Configures camera to follow the user location indicator.

    Note

    It’s recommended to use only the default animation option for transition to the followPuck viewport, because it handles the moving user location puck. Other animation options such as easeIn, easeOut, easeInOut, linear, or fly don’t support this.

    Declaration

    Swift

    @_documentation(visibility: public)
    public static func followPuck(zoom: CGFloat,
                                  bearing: FollowPuckViewportStateBearing = .constant(0),
                                  pitch: CGFloat = 0) -> Viewport

    Parameters

    zoom

    Zoom level of the map.

    bearing

    Bearing of the map.

    pitch

    Pitch toward the horizon measured in degrees, with 0 degrees resulting in a top-down view for a two-dimensional map.

    Return Value

    A viewport configured to follow the user location puck.

  • Creates a new MapViewport with modified inset options.

    Insets are ignored for idle viewport.

    By default, insets are equal to the safe area. This method allows you to set additional insets, or ignore safe area part on the specified edges.

    Declaration

    Swift

    @_documentation(visibility: public)
    public func inset(by insets: SwiftUI.EdgeInsets, ignoringSafeArea: Edge.Set = []) -> Viewport

    Parameters

    insets

    Additional insets, that will be summarized with existing safe area insets.

    ignoringSafeArea

    A set of edges where safe area’s contribution to the resulting inset should be ignored.

    Return Value

    A viewport with modified inset options.

  • Creates a new MapViewport with modified inset options.

    Insets are ignored for idle viewport.

    By default, insets are equal to the safe area. This method allows you to set additional inset for the specified edges. This method can be called multiple times to configure different edges.

    Declaration

    Swift

    @_documentation(visibility: public)
    public func inset(edges: Edge.Set, length: CGFloat, ignoringSafeArea: Bool = false) -> Viewport

    Parameters

    edges

    Edges for which to set the additional inset.

    length

    The length of inset.

    ignoringSafeArea

    If safe area’s contribution should be ignored for the specified edges.

    Return Value

    A viewport with modified inset options.

  • Is true when viewport is idle.

    Declaration

    Swift

    @_documentation(visibility: public)
    public var isIdle: Bool { get }
  • Is true when camera is configured from the default style camera properties.

    Declaration

    Swift

    @_documentation(visibility: public)
    public var isStyleDefault: Bool { get }
  • Returns the camera options if viewport is configured with camera options.

    Note

    The CameraOptions is ignored, it is replaced with insetOptions, see inset(by:ignoringSafeArea:).

    Declaration

    Swift

    @_documentation(visibility: public)
    public var camera: CameraOptions? { get }
  • Returns the overview options if viewport is configured to overview the specified geometry.

    Declaration

    Swift

    @_documentation(visibility: public)
    public var overview: OverviewOptions? { get }
  • Returns the follow puck options if viewport is configured to follow the user location puck.

    Declaration

    Swift

    @_documentation(visibility: public)
    public var followPuck: FollowPuckOptions? { get }