Mapbox Maps SDK for iOS
repository·main·Indexed 20 days ago
https://github.com/mapbox/mapbox-maps-iosA native library for rendering interactive, customizable maps using Metal, based on the Mapbox Style and Vector Tile specifications. Includes a SwiftUI interface via the MapboxMapsSwiftUI module (currently in development) and tools for API compatibility checking using breaking-api-check.py and swift-api-digester.
What's inside mapbox-maps-ios
- The Mapbox Maps SDK for iOS is a public library designed for displaying interactive, highly customizable maps in native iOS applications. It renders map styles that follow the Mapbox Style Specification using vector tiles that conform to the Mapbox Vector Tile Specification, utilizing Metal for high-performance rendering.
Core Maps API Overview
mainThe Core Maps API (also known as GL-Native) is the primary interface for rendering maps within the Mapbox Maps iOS SDK. It provides the fundamental capabilities for map display, camera control, and offline data management.Use Mapbox Maps SwiftUI
mainThe
MapboxMapsSwiftUImodule provides a SwiftUI interface for the Mapbox Maps SDK, designed to simplify building Mapbox-powered applications using SwiftUI.⚠️ Caution: This module is currently in development. Its API is not stable, and it is strongly recommended that it not be used in production.
Use StylePack for managing map styles
mainAStylePackis used to manage the loading and lifecycle of map styles. It allows you to load style information and assets required to render a map. You can useStylePackLoadOptionsto configure the loading process and monitor progress throughStylePackLoadProgress.Add custom layers to Mapbox Standard using Slots
mainWhen using the Mapbox Standard style, you can place custom layers into specific, stable locations called
slots. This ensures your layers remain correctly positioned even as the basemap evolves.Available slots:
bottom: Above polygons (land, water, etc.).middle: Above lines (roads) and behind 3D buildings.top: Above POI labels and behind Place/Transit labels.- not specified: Above all existing layers.
Note: For the Standard style, you can only add layers to these three specific slots (
bottom,middle,top) within the basemap.var layer = LineLayer(id: "line-layer", source: "line-source") layer.slot = .middle mapView.mapboxMap.addLayer(layer)Manage the viewport
mainThe Viewport system controls what part of the map is visible and how the camera behaves relative to specific targets (like a 'puck' or a user location).
Core concepts:
ViewportManager: The central authority for managing viewport states.ViewportState: Represents a specific view configuration. Common states include:FollowPuckViewportState: Automatically follows a moving object (like a user's location).OverviewViewportState: Provides a high-level overview of a specific area.
ViewportTransition: Defines how the map moves between differentViewportStateconfigurations (e.g.,DefaultViewportTransitionorImmediateViewportTransition).ViewportStatusObserver: Allows you to react to changes in the viewport's status or reason for change (ViewportStatusChangeReason).
Manage map ornaments with OrnamentsManager
mainTheOrnamentsManageris the primary interface for controlling the visibility and configuration of map ornaments, such as the attribution button, logo, compass, and scale bar. You can use it to customize the appearance and positioning of these UI elements on the map.Import styles via Style JSON
mainThe Style API allows you to import other styles (like Mapbox Standard) into your main style by reference. This is done by adding an
importssection to your Style JSON. Configuration properties for the imported style can then be adjusted at runtime via theStyleManagerobject.Key
StyleManagermethods for managing imports:styleImports: Returns all imported styles.removeStyleImport(for:): Removes a style import by ID.getStyleImportSchema(for:): Returns the schema for an imported style.getStyleImportConfigProperties(for:): Returns available configuration properties.getStyleImportConfigProperty(for:config:): Gets a specific property.setStyleImportConfigProperties(for:configs:): Sets multiple properties.setStyleImportConfigProperty(for:config:value:): Sets a single property.
"imports": [ { "id": "A", "url": "STYLE_URL_FOR_A", "config": { "font": "Montserrat", "lightPreset": "dusk", "showPointOfInterestLabels": true, "showTransitLabels": false, "showPlaceLabels": true, "showRoadLabels": false } } ]Add interactive elements with eye-tracking feedback on visionOS
mainBecause Mapbox Map renders content in Metal, standard map features like symbols, lines, and polygons do not support the native visionOS
hoverEffect(eye-tracking visual feedback).To provide interactive elements that respond to user gaze, use View Annotations instead of standard point annotations. View annotations allow you to place SwiftUI views on the map that can utilize
.hoverEffect().Map { // Standard point annotations handle gestures but lack eye-tracking feedback PointAnnotation(coordinate: coordinate1) .onTapGesture { print("point annotation tapped") } // View annotations allow for visual eye-tracking feedback via SwiftUI modifiers MapViewAnnotation(coordinate: coordinate2) { Circle() .fill(.blue) .hoverEffect() .onTapGesture { print("view annotation tapped") } } }Animate the camera
mainCamera animations allow for smooth transitions between different camera states (position, zoom, bearing, pitch).
Key components include:
CameraAnimationsManager: Manages the lifecycle of camera animations.CameraAnimator: The interface for controlling animations.FlyToCameraAnimator: A specific animator used to 'fly' the camera to a new location.CameraTransition: Defines the movement from one state to another.TimingCurve: Controls the pacing of the animation (e.g., easing).AnimationCompletion: A callback mechanism to handle logic once an animation finishes.
Use TileStore for managing tile data
mainTheTileStoreis the primary interface for managing tile data, including downloading, caching, and retrieving tiles. It works in conjunction withTileRegionto manage specific geographic areas andTilesetDescriptorto define which tilesets are being accessed. You can monitor progress usingTileRegionLoadProgressand observe changes viaTileStoreObserver.Configure ornament options and positioning
mainWhen customizing ornaments, you use several supporting types to define their behavior:
OrnamentOptions: A configuration object used to set properties for various ornaments.OrnamentPosition: Defines where an ornament is placed on the map (e.g., corners).OrnamentVisibility: Controls whether an ornament is visible or hidden.AttributionButtonOptions,CompassViewOptions,LogoViewOptions, andScaleBarViewOptions: Specific configuration objects for individual ornament types.