Official integration patterns for Mapbox Maps SDK on iOS. Covers installation, adding markers, user location, custom data, styles, camera control, and featureset interactions. Based on official Mapbox documentation.
在 iOS 上使用 Swift、SwiftUI 和 UIKit 集成 Mapbox Maps SDK v11 的官方模式。
使用此技能的场景:
官方资源:
将您的公共令牌添加到 Info.plist:
xml
获取令牌: 登录 mapbox.com
备选方案: CocoaPods 或直接下载(安装指南)
基础地图:
swift
import SwiftUI
import MapboxMaps
struct ContentView: View {
@State private var viewport: Viewport = .camera(
center: CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194),
zoom: 12
)
var body: some View {
Map(viewport: $viewport)
.mapStyle(.standard)
}
}
带装饰组件:
swift
Map(viewport: $viewport)
.mapStyle(.standard)
.ornamentOptions(OrnamentOptions(
scaleBar: .init(visibility: .visible),
compass: .init(visibility: .adaptive),
logo: .init(position: .bottomLeading)
))
swift
import UIKit
import MapboxMaps
class MapViewController: UIViewController {
private var mapView: MapView!
override func viewDidLoad() {
super.viewDidLoad()
let options = MapInitOptions(
cameraOptions: CameraOptions(
center: CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194),
zoom: 12
)
)
mapView = MapView(frame: view.bounds, mapInitOptions: options)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(mapView)
mapView.mapboxMap.loadStyle(.standard)
}
}
点注释是在地图上标记位置最常用的方式。
SwiftUI:
swift
Map(viewport: $viewport) {
PointAnnotation(coordinate: CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194))
.iconImage(custom-marker)
}
UIKit:
swift
// 创建注释管理器(一次创建,重复使用进行更新)
var pointAnnotationManager = mapView.annotations.makePointAnnotationManager()
// 创建标记
var annotation = PointAnnotation(coordinate: CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194))
annotation.image = .init(image: UIImage(named: marker)!, name: marker)
annotation.iconAnchor = .bottom
// 添加到地图
pointAnnotationManager.annotations = [annotation]
多个标记:
swift
let locations = [
CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194),
CLLocationCoordinate2D(latitude: 37.7849, longitude: -122.4094),
CLLocationCoordinate2D(latitude: 37.7649, longitude: -122.4294)
]
let annotations = locations.map { coordinate in
var annotation = PointAnnotation(coordinate: coordinate)
annotation.image = .init(image: UIImage(named: marker)!, name: marker)
return annotation
}
pointAnnotationManager.annotations = annotations
第 1 步:在 Info.plist 中添加位置权限:
xml
第 2 步:请求权限并显示位置:
swift
import CoreLocation
// 请求权限
let locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()
// 显示用户位置指示器
mapView.location.options.puckType = .puck2D()
mapView.location.options.puckBearingEnabled = true
swift
// ❌ 不要重复创建新的管理器
func updateMarkers() {
let manager = mapView.annotations.makePointAnnotationManager()
manager.annotations = markers
}
// ✅ 一次创建,重复使用
let pointAnnotationManager: PointAnnotationManager
init() {
pointAnnotationManager = mapView.annotations.makePointAnnotationManager()
}
func updateMarkers() {
pointAnnotationManager.annotations = markers
}
swift
// ✅ 一次性全部更新
pointAnnotationManager.annotations = newAnnotations
// ❌ 不要逐个更新
for annotation in newAnnotations {
pointAnnotationManager.annotations.append(annotation)
}
swift
// 在闭包中使用 weak self
mapView.gestures.onMapTap.observe { [weak self] context in
self?.handleTap(context.coordinate)
}.store(in: &cancelables)
// 在 deinit 中清理
deinit {
cancelables.forEach { $0.cancel() }
}
swift
// ✅ 标准样式经过优化,推荐使用
.mapStyle(.standard)
// 仅在特定用例需要时使用其他样式
.mapStyle(.standardSatellite) // 卫星影像
检查:
swift
mapView.mapboxMap.onStyleLoaded.observe { [weak self] _ in
print(样式加载成功)
// 在此处添加图层和数据源
}.store(in: &cancelables)
当任务需要更深入的模式时,加载以下参考文件:
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 mapbox-ios-patterns-1775921528 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 mapbox-ios-patterns-1775921528 技能
skillhub install mapbox-ios-patterns-1775921528
文件大小: 11.13 KB | 发布时间: 2026-4-12 10:30