本项目启动于 iOS 17 的 MapKit SwiftUI 之前,iOS 17 的 SwiftUI 已经在很大程度上消除了对该库的需求。我可能不会对本项目进行进一步的更改。有关更多信息,请参阅此 wwdc 视频。
一个 MKMapView 的封装器,具有比 Map 更多的功能。
点和覆盖物 | 相机 | 样式 |
---|---|---|
![]() |
![]() |
![]() |
特性 | AdvancedMap |
MapKit.Map |
---|---|---|
带有地图坐标传递到处理程序的点击/长按手势。 | ✅ | ❌ |
支持拖放的注释 | ✅ (UIKit 注释视图) |
✅ (SwiftUI 注释视图) |
覆盖物 | ✅ (UIKit 覆盖物视图) |
❌ |
为 UI 覆盖物指定 EdgeInsets | ✅ | ❌ |
显示用户位置 | ✅ (通过 AnnoatationViewFactory ) |
✅ (作为初始化参数) |
区域状态更改处理程序,一个回调,告知地图更改动画是否正在进行。 | ✅ | ❌ |
绑定到可选的地图区域,以便地图最初定位在国家/地区边界框周围。 | ✅ | ❌ |
MKMapCamera 支持 |
✅ | ❌ |
struct TappableMap: View {
var body: some View {
// `.constant(nil)` uses MKMapView's behavior of starting the map over the phone's current country.
// Still scrollable by default.
AdvancedMap(mapRect: .constant(nil))
.onTapOrClickMapGesture { coordinate in
print("Tapped map at: \(coordinate)")
}
}
}
struct TappableMap: View {
static let annotationViewFactory = AnnotationViewFactory(
register: { mapView in
mapView.register(MKMarkerAnnotationView.self, forAnnotationViewWithReuseIdentifier: String(describing: MKPointAnnotation.self))
},
view: { mapView, annotation in
mapView.dequeueReusableAnnotationView(withIdentifier: String(describing: MKPointAnnotation.self), for: annotation)
}
)
@State var mapVisibility: MapVisibility?
@State var annotations: [MKPointAnnotation] = [MKPointAnnotation]()
var body: some View {
AdvancedMap(mapVisibility: $mapVisibility)
.annotations(annotations, annotationViewFactory: Self.annotationViewFactory)
.onTapOrClickMapGesture { coordinate in
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotations.append(annotation)
}
}
}
灵感来自以下项目,有时也会从中借鉴: