ios - 如何从 MKClusterAnnotation 获取具有相同坐标的注释?
问题描述
我目前正在开发一个应用程序,我在其中使用来自苹果的 mapkit 的 MKClusterAnnotations 加载不同类型的注释。我们知道 MKClusterAnotation 是An annotation that groups two or more distinct annotations into a single entity
. 这是否可以从具有相同坐标的集群中获取或删除注释?
例如,如果有 2 个具有相同坐标的集群,比如lat: 45.757036789379, long: 4.8989974471952
另一个集群lat: 45.757049657763, long: 4.8989858812774
,那么当我放大或选择集群时,它应该从集群中删除,否则它应该从集群成员注释中获取。
在 Map DidSelect 委托下方,
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
if let annotation = view.annotation, annotation.isKind(of: MKClusterAnnotation.self) {
let cluster: MKClusterAnnotation = annotation as! MKClusterAnnotation
if cluster.memberAnnotations.count > 1 {
mapView.showAnnotations(cluster.memberAnnotations, animated: true)
self.isMapDidSelect = true
} else { //In this case need to fetch memberAnnotations when cluster fails to remove the grouped annotations itself
} } }
而且我还在视图上为注释委托设置了集群标识符,
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation.isKind(of: MKUserLocation.self) {
let annotationView: CurrentUserAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: CurrentUserAnnotationView.identifier(), for: annotation) as! CurrentUserAnnotationView
annotationView.selectedMapVisibility = viewModel.selectedMapVisibility.value
annotationView.photoUrl = viewModel.user.value?.photoUrl
return annotationView
}
if annotation.isKind(of: MKClusterAnnotation.self) {
if let annotationView: MKAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: MKMapViewDefaultClusterAnnotationViewReuseIdentifier) as? ClusterAnnotationView {
return annotationView
}
}
let annotationView = mapView.annotationViewFor(annotation: annotation)
annotationView?.clusteringIdentifier = "clusterId"
return annotationView
}
解决方案
推荐阅读
- python - Matplotlib 堆积条形图:需要交换 x 和高度
- dialogflow-es - Dialogflow 实体自动扩展
- c# - 如何使用按钮在 WPF 上启用和禁用鼠标单击事件
- c - 如何在虚拟接口上发送多播报告?
- java - 问题,切换到另一个片段后,片段数据被清除
- android - 具有相同对象的嵌套 RecyclerView
- java - 何时使用 Optional.orElse() 而不是 Optional.orElseGet()
- c# - toCharArray() Python 等价物
- go - 如何捕获输出并将其发送到文件
- c# - 如何仅反序列化 json 文档的一部分?