我正在处理一个应用程序,我必须将
MKMapView
(UIKit) 嵌入到 SwiftUI 视图中。不幸的是我不能使用 SwiftUI 实现的 Map
,因为我必须支持集群(Map
目前还不能做到)。MKMapView
不反映应用于数据源(persons
数组)的更改。
内容视图
struct ContentView: View {
@State private var persons: [Person] = [
.init(
coordinate: .init(latitude: 50.7123012, longitude: 5.1231021),
title: "Person 1"
),
.init(
coordinate: .init(latitude: 49.3123012, longitude: 5.1231021),
title: "Person 2"
)
]
var body: some View {
VStack {
MapView(persons: $persons)
Button("Remove Person at index 0") {
persons.remove(at: 0)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
在
MKMapView
的帮助下实现UIViewRepresentable
:
struct MapView: UIViewRepresentable {
@Binding var persons: [Person]
private let mapView = MKMapView()
init(persons: Binding<[Person]>) {
self._persons = persons
// other stuff
}
func makeUIView(context: Context) -> MKMapView {
mapView.delegate = context.coordinator
mapView.register(MKMarkerAnnotationView.self, forAnnotationViewWithReuseIdentifier: "testAnnotation")
return mapView
}
func updateUIView(_ uiView: MKMapView, context: Context) {
DispatchQueue.main.async {
mapView.removeAnnotations(mapView.annotations)
mapView.addAnnotations(persons)
}
}
}
extension MapView {
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, MKMapViewDelegate {
var parent: MapView
init(_ parent: MapView) {
self.parent = parent
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "testAnnotation", for: annotation) as? MKMarkerAnnotationView {
annotationView.glyphImage = UIImage(systemName: "person")
return annotationView
}
return nil
}
}
}
演示模型:
class Person: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
let title: String?
init(coordinate: CLLocationCoordinate2D, title: String?) {
self.coordinate = coordinate
self.title = title
}
}
请注意,这是一个简化的实现,我删除了聚类等逻辑以专注于问题。
如果您点击“删除索引 0 处的人员”,它实际上会删除索引 0 处的条目,但不幸的是地图并未更新。问题是
updateUIView
中的 MapView
没有被调用,我不确定为什么会出现这种情况。有两个问题在内部创建地图视图
makeView
- 这是一个设计的地方,在更新中更新注释(不需要延迟),这是一个调用绑定更新来精确更新 UIView 对应项的地方(所有 UI 失效/刷新都是进行的)自动)
所以,这是固定版本。使用 Xcode 13.4 / iOS 15.5 进行测试
struct MapView: UIViewRepresentable {
@Binding var persons: [Person]
init(persons: Binding<[Person]>) {
self._persons = persons
// other stuff
}
func makeUIView(context: Context) -> MKMapView {
let mapView = MKMapView() // << here !!
mapView.delegate = context.coordinator
mapView.register(MKMarkerAnnotationView.self, forAnnotationViewWithReuseIdentifier: "testAnnotation")
return mapView
}
func updateUIView(_ mapView: MKMapView, context: Context) {
mapView.removeAnnotations(mapView.annotations) // << here !!
mapView.addAnnotations(persons)
}
}
您可以将 .id(UUID()) 添加到 MapView(persons: $persons) 中,以重绘视图。