在 UIRepresentable 中向 MapKit 添加注释
Adding annotations to MapKit in UIRepresentable
我正在使用 SwiftUI 并使用 MKMapView 在视图上显示地图、注释和叠加层。
我使用的是新的 Map()
,但由于缺乏覆盖支持,我又回到了 UIKit Representable。
当我使用 Map
时,添加注释很容易,但是当使用 UIKitRepresentable
时,我对将数据放在哪里以及如何从数组进行注释感到有点困惑从网络调用中拉出。
我读过的所有内容都是在 Obj-C 中或添加单个注释点。我正在尝试添加(目前 ~800),这就是为什么我想利用 MKMapView 的可重用性和集群。
这是我目前拥有的:
struct UIMapView: UIViewRepresentable {
@EnvironmentObject var dataModel: DataModel
@EnvironmentObject var mapViewModel: MapViewModel
func makeCoordinator() -> Coordinator { Coordinator() }
func makeUIView(context: Context) -> MKMapView {
let view = mapViewModel.mapView
drawOverlayRing(view: view)
view.delegate = context.coordinator
return view
}
func updateUIView(_ uiView: MKMapView, context: Context) {}
class Coordinator: NSObject, MKMapViewDelegate {
func mapView(_ map: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation { return nil }
let identifier = "pinAnnotation"
var annotationView = map.dequeueReusableAnnotationView(
withIdentifier: identifier
) as? MKPinAnnotationView
if annotationView == nil {
annotationView = MKPinAnnotationView(
annotation: annotation,
reuseIdentifier: identifier
)
annotationView?.canShowCallout = true
} else {
annotationView?.annotation = annotation
}
return annotationView
}
}
func getAnnotations(view: MKMapView) {
for location in dataModel.locations {
let annotation = MKPointAnnotation()
annotation.title = location.title
annotation.coordinate = CLLocationCoordinate2D(
latitude: location.latitude,
longitude: location.longitude
)
view.addAnnotation(annotation)
}
}
}
我有时会在 updateUIView 中添加注解,像这样:
updateUIView() {
// remove the old ones --> uiView.removeAnnotations(uiView.annotations)
uiView.addAnnotations(toMapAnnotations(locations: dataModel.locations))
}
func toMapAnnotations(locations: [CLLocationCoordinate2D]) -> [MapAnnotation] {
return locations.map { MapAnnotation(location: [=10=]) }
}
final class MapAnnotation: NSObject, MKAnnotation {...}
由于updateUIView
被多次调用,稍后会提供代码。
我认为最好避免调用 addAnnotations
.
我正在使用 SwiftUI 并使用 MKMapView 在视图上显示地图、注释和叠加层。
我使用的是新的 Map()
,但由于缺乏覆盖支持,我又回到了 UIKit Representable。
当我使用 Map
时,添加注释很容易,但是当使用 UIKitRepresentable
时,我对将数据放在哪里以及如何从数组进行注释感到有点困惑从网络调用中拉出。
我读过的所有内容都是在 Obj-C 中或添加单个注释点。我正在尝试添加(目前 ~800),这就是为什么我想利用 MKMapView 的可重用性和集群。
这是我目前拥有的:
struct UIMapView: UIViewRepresentable {
@EnvironmentObject var dataModel: DataModel
@EnvironmentObject var mapViewModel: MapViewModel
func makeCoordinator() -> Coordinator { Coordinator() }
func makeUIView(context: Context) -> MKMapView {
let view = mapViewModel.mapView
drawOverlayRing(view: view)
view.delegate = context.coordinator
return view
}
func updateUIView(_ uiView: MKMapView, context: Context) {}
class Coordinator: NSObject, MKMapViewDelegate {
func mapView(_ map: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation { return nil }
let identifier = "pinAnnotation"
var annotationView = map.dequeueReusableAnnotationView(
withIdentifier: identifier
) as? MKPinAnnotationView
if annotationView == nil {
annotationView = MKPinAnnotationView(
annotation: annotation,
reuseIdentifier: identifier
)
annotationView?.canShowCallout = true
} else {
annotationView?.annotation = annotation
}
return annotationView
}
}
func getAnnotations(view: MKMapView) {
for location in dataModel.locations {
let annotation = MKPointAnnotation()
annotation.title = location.title
annotation.coordinate = CLLocationCoordinate2D(
latitude: location.latitude,
longitude: location.longitude
)
view.addAnnotation(annotation)
}
}
}
我有时会在 updateUIView 中添加注解,像这样:
updateUIView() {
// remove the old ones --> uiView.removeAnnotations(uiView.annotations)
uiView.addAnnotations(toMapAnnotations(locations: dataModel.locations))
}
func toMapAnnotations(locations: [CLLocationCoordinate2D]) -> [MapAnnotation] {
return locations.map { MapAnnotation(location: [=10=]) }
}
final class MapAnnotation: NSObject, MKAnnotation {...}
由于updateUIView
被多次调用,稍后会提供代码。
我认为最好避免调用 addAnnotations
.