首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >集群时显示MKAnnotation标注

集群时显示MKAnnotation标注
EN

Stack Overflow用户
提问于 2017-09-20 19:47:54
回答 1查看 1.9K关注 0票数 3

我有一个TableView,用于在单元格被点击时显示MapView注释标注。

在iOS 10中,我可以将MapView集中在一个注释上,然后使用以下方法显示它的标注:

代码语言:javascript
复制
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let location = locations[indexPath.item]
    mapView.setCenter(location.coordinate, animated: true)
    mapView.selectAnnotation(location, animated: true)
}

locationsMKAnnotation的数组,在iOS 10上使用MKPinAnnotationViews,在iOS 11上使用MKMarkerAnnotationViews。

iOS 11在缩放地图时会自动隐藏和显示MKMarkerAnnotationViews。

这有一个不幸的副作用,阻止.selectAnnotation()可靠地工作,因为标记仍然可以隐藏在地图的中心。

我见过医生也明白为什么:

如果指定的注释不在屏幕上,因此没有关联的注释视图,则此方法没有任何效果。

是否有一种禁用注释群集/隐藏的方法?还是以某种方式强制所选注释可见?

EN

回答 1

Stack Overflow用户

发布于 2017-09-20 22:30:28

您可以将displayPriority of a MKMarkerAnnotationView设置为rawValue of 1000,将不那么有趣的MKMarkerAnnotationView's displayPriority设置为更低的值,这将导致标记注释优先于其他注释。

在您的示例中,您希望保存对要选择的注释的引用,从map视图中删除该注释并再次添加它。这将导致map视图再次请求注释的视图,您可以调整优先级,使其高于其周围的注释。例如:

代码语言:javascript
复制
    func showAnnotation()
    {
        self.specialAnnotation = annotations.last
        self.mapView.removeAnnotation(self.specialAnnotation)
        self.mapView.addAnnotation(self.specialAnnotation)
        self.mapView.setCenter(self.specialAnnotation.coordinate, animated: true)
        self.mapView.selectAnnotation(self.specialAnnotation, animated: true)
    }

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?
    {
        let markerView = mapView.dequeueReusableAnnotationView(withIdentifier: "Marker", for: annotation) as? MKMarkerAnnotationView
        let priority = (annotation as? Annotation) == self.specialAnnotation ? 1000 : 500
        markerView?.displayPriority = MKFeatureDisplayPriority(rawValue: priority)
        // optionally change the tint color for the selected annotation
        markerView?.markerTintColor = priority == 1000 ? .blue : .red
        return markerView
    }

其中specialAnnotation是一个符合MKAnnotation的对象。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46330639

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档