ios - 快速拖动地图或更改缩放级别时,MapKit 上的自定义集群注释崩溃
问题描述
在实现了对自定义注释进行集群的方法之后,只要通过滚动或更改缩放级别快速调整地图视图,应用程序就会崩溃。
-[MKPointAnnotation memberAnnotations]:无法识别的选择器发送到实例 0x281396c00
我的猜测是编译器正在尝试检索注释信息,但找不到数据。由于我对 Swift 还很陌生,所以我看不出我缺少什么。您的帮助将不胜感激。
我有一个非常基本的设置来在 SwiftUI 中显示地图。在主文件中,我从MapView.swift调用 MapView
struct MapView: UIViewRepresentable {
@ObservedObject var store = DataStoreMap()
func makeCoordinator() -> MapViewCoordinator {
MapViewCoordinator(self)
}
func makeUIView(context: Context) -> MKMapView{
MKMapView(frame: .zero)
}
func updateUIView(_ view: MKMapView, context: Context){
let location = getUserLocation()
let chargers = store.chargers
let coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
let span = MKCoordinateSpan(latitudeDelta: 0.03, longitudeDelta: 0.03)
let region = MKCoordinateRegion(center: coordinate, span: span)
view.setRegion(region, animated: true)
for charger in chargers {
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: charger.addressInfo.latitude, longitude: charger.addressInfo.longitude)
view.delegate = context.coordinator
view.addAnnotation(annotation)
}
}
}
同一个文件中还包含我的自定义注释类。
class MapViewCoordinator: NSObject, MKMapViewDelegate {
var mapViewController: MapView
init(_ control: MapView) {
self.mapViewController = control
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
//Custom View for Annotation
var annotationView = MKMarkerAnnotationView()
annotationView.canShowCallout = true
let identifier = "laadpaal"
if let dequedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKMarkerAnnotationView {
annotationView = dequedView
} else {
annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
}
annotationView.markerTintColor = .some(.systemBlue)
annotationView.glyphImage = UIImage(named: "car1")
annotationView.glyphTintColor = .yellow
annotationView.clusteringIdentifier = identifier
return annotationView
}
}
解决方案
崩溃的原因是您没有考虑地图套件请求的其他注释(例如MKUserLocation
)。clusteringIdentifier
由于设置为非nil
值时的自动聚类,您正在触发此操作。
nil
当您想自己处理注释时只需返回,因此MKMapView
使用默认处理:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation { return nil }
if annotation is MKClusterAnnotation { return nil }
let view = mapView.dequeueReusableAnnotationView(withIdentifier: "identifier", for: annotation)
view.clusteringIdentifier = "clusterIdentifer"
// …
return view
}
如果您想自定义集群注释,只需为MKClusterAnnotation
. nil
如果您显示用户位置,MKUserLocation
如果您想要默认的蓝点,请不要忘记返回。
推荐阅读
- c# - MediaPlayerElement 不播放视频,除了不从我的可移动设备中获取路径
- docker - 未定义时,Docker Compose env 文件优先于 shell 传递变量
- python-2.7 - 如何验证对 AWS 签名 v4 的支持?
- firebase - Cloud Functions 部署失败:“加载用户代码时函数失败。错误消息:无法加载文件 lib/index.js 中的代码。”
- amazon-web-services - Nodejs 应用程序未连接到亚马逊 ecs 任务定义中的 redis。getaddrinfo ENOTFOUND redis redis:6379
- java - 无法从 Mapbox FeatureCollection 获取地址
- javascript - 为什么页面更改时 DOM 会自动删除我的类?
- kotlin - sortedDescending() 和 sortedByDescending() 有什么区别?
- multithreading - Erlang timer:sleep(1000) 导致线程死机
- javascript - 是否有外部 api 来获取英国夏令时格式的当前日期/时间