gpt4 book ai didi

ios - 集群时显示 MKAnnotation 标注

转载 作者:搜寻专家 更新时间:2023-10-31 22:41:15 31 4
gpt4 key购买 nike

我有一个 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)
}

locations 是一个 MKAnnotation 数组。我在 iOS 10 上使用 MKPinAnnotationView,在 iOS 11 上使用 MKMarkerAnnotationView

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

enter image description here

不幸的是,这会阻止 .selectAnnotation() 可靠地工作,因为标记在 map 居中后仍可能隐藏。

我看过文档并了解原因:

If the specified annotation is not onscreen, and therefore does not have an associated annotation view, this method has no effect.

有没有办法禁用注释聚类/隐藏?或者以某种方式强制显示所选注释?

最佳答案

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

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

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

关于ios - 集群时显示 MKAnnotation 标注,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46330639/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com