简体   繁体   English

集群时显示 MKAnnotation 标注

[英]Showing MKAnnotation callout when clustered

I have a TableView that is used to show MapView annotation callouts when the cells are tapped.我有一个 TableView,用于在点击单元格时显示 MapView 注释标注。
In iOS 10 I can centre the MapView on an annotation then show it's callout using:在 iOS 10 中,我可以将 MapView 放在注释的中心,然后使用以下方法显示它的标注:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let location = locations[indexPath.item]
    mapView.setCenter(location.coordinate, animated: true)
    mapView.selectAnnotation(location, animated: true)
}

locations is an array of MKAnnotation s. locations是一个MKAnnotation数组。 I'm using MKPinAnnotationView s on iOS 10 and MKMarkerAnnotationView s on iOS 11.我使用MKPinAnnotationView iOS上的10和S MKMarkerAnnotationView S于iOS的11。

iOS 11 automatically hides and shows MKMarkerAnnotationViews as you zoom the map.当您缩放地图时,iOS 11 会自动隐藏和显示 MKMarkerAnnotationViews。

在此处输入图片说明

This has the unfortunate side effect of preventing .selectAnnotation() from working reliably because the marker could still be hidden after centering the map.这具有阻止.selectAnnotation()可靠工作的不幸副作用,因为在将地图居中后标记仍可能被隐藏。

I've seen the docs and understand why:我看过文档并理解为什么:

If the specified annotation is not onscreen, and therefore does not have an associated annotation view, this method has no effect.如果指定的注释不在屏幕上,因此没有关联的注释视图,则此方法无效。

Is there a way to disable annotation clustering/hiding?有没有办法禁用注释聚类/隐藏? Or some way to force the selected annotation to be visible?或者某种方式强制所选注释可见?

You can set the displayPriority of an MKMarkerAnnotationView to a rawValue of 1000 and the less interesting MKMarkerAnnotationView 's displayPriority to something lower.您可以将MKMarkerAnnotationViewdisplayPriority设置为1000的 rawValue 并将不太有趣的MKMarkerAnnotationViewdisplayPriority为较低的值。 This will cause that marker annotation to take precedence over others.这将导致该标记注释优先于其他标记。

In your case, you will like want to hold a reference to the annotation that you would like to select, remove that annotation from the map view and add it again.在您的情况下,您希望保留对要选择的注释的引用,从地图视图中删除该注释并再次添加。 This will cause the map view to request a view for the annotation again and you can adjust the priority so that it is higher than the annotations around it.这将导致地图视图再次请求注释视图,您可以调整优先级,使其高于周围的注释。 For example:例如:

    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
    }

Where specialAnnotation is an object that conforms to MKAnnotation .其中specialAnnotation是符合MKAnnotation的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM