MKMapView 在 SwiftUI 中的应用。
对于 iOS 14 及更高版本,请使用 Apple 官方的 Map 视图。
使用 Xcode,选择 File
-> Swift Packages
-> Add Package Dependency
并输入 https://github.com/sgade/swiftui-mapview
。
在您的视图中,添加地图。请参阅示例项目,了解如何集成地图视图。
import SwiftUIMapView
struct ContentView: View {
var body: some View {
MapView()
}
}
MapView(mapType: .standard)
当前位置可以显示在地图上。默认情况下,此项为 true
。请注意,应用程序需要获得访问当前用户位置的权限。有关更多信息,请参阅 MapView.showsUserLocation
的文档。
MapView(showsUserLocation: true)
MapView(userTrackingMode: .follow)
为 center
传入的绑定定义了可见区域。将其设置为 nil
将在加载时使用地图的默认区域。当可见区域更改时,它也会更新。
@State var center: CLLocationCoordinate2D?
MapView(center: self.$center)
为 zoom
传入的绑定定义了可见区域跨度。如果您未指定任何值,则使用默认缩放。当可见区域更改时,它也会更新。
@State var zoom: MKCoordinateSpan
MapView(zoom: self.$zoom)
注释表示为实现了 MapViewAnnotation
协议的自定义类的对象。从诸如 MKPlacemark
之类的现有类进行子类化可能会有所帮助。
let annotations: [MapViewAnnotation] = ...
MapView(annotation: self.annotations)
可以通过绑定传入选定注释的列表。选择注释会更新绑定,反之亦然。
@State var selectedAnnotations: [MapViewAnnotation] = []
MapView(selectedAnnotations: self.$selectedAnnotations)
请参阅贡献指南。
本项目根据 MIT 许可证的条款获得许可。有关更多信息,请参阅 LICENSE。