【问题标题】:Showing MKAnnotation callout when clustered集群时显示 MKAnnotation 标注
【发布时间】:2017-09-20 19:47:54
【问题描述】:

我有一个 TableView,用于在点击单元格时显示 MapView 注释标注。
在 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)
}

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

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

这具有阻止.selectAnnotation() 可靠工作的不幸副作用,因为标记在地图居中后仍可能被隐藏。

我已查看文档并理解原因:

如果指定的注释不在屏幕上,因此不会 有关联的注解视图,此方法无效。

有没有办法禁用注释集群/隐藏? 或者有什么方法可以强制选中的注解可见?

【问题讨论】:

  • 这不是一个解决方案,但可能是一个解决方法的想法:尝试以编程方式将地图缩放到更放大的状态(您可能可以使用缩放因子来获得它,以便注释不重叠)在您将地图居中后。
  • 嗨@aksh1t 我考虑过这个,但我不想弄乱用户选择的缩放级别。另外,我的一些注释可能非常接近,因此在某些情况下这实际上可能不起作用。
  • 这是有道理的。这是我在查看其他一些 mapview 方法时得到的另一个想法:尝试使用您的注释调用 showAnnotations: 方法,然后执行 selectAnnotation。 (我不知道 showAnnotation 方法是否改变了缩放级别;它可能确实改变了缩放级别)。
  • showAnnotations 确实会影响缩放。

标签: ios swift mapkit mkannotation ios11


【解决方案1】:

您可以将MKMarkerAnnotationViewdisplayPriority 设置为1000 的rawValue,并将不太有趣的MKMarkerAnnotationViewdisplayPriority 设置为更低的值。这将导致该标记注释优先于其他注释。

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

    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 的对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-27
    • 2015-08-27
    相关资源
    最近更新 更多