2017-09-20 66 views
2

我有一个TableView,用于在单击细胞时显示MapView注释标注。
在iOS中10 I可以集中在一个注解的MapView然后显示它使用的标注:显示群集时的MKAnnotation标注

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上使用MKPinAnnotationView s,在iOS 11上使用MKMarkerAnnotationView s。

iOS 11在缩放地图时自动隐藏并显示MKMarkerAnnotationView。

enter image description here

这具有防止.selectAnnotation()从工作可靠,因为标记仍然为中心的地图后,被隐藏的不幸的副作用。

我见过的文档和理解为什么:

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

有没有办法可以禁用注释集群/隐藏? 或者某种方式强制选定的注释可见?

+0

这不是一个解决方案,但可能是一种解决方法的想法:尝试以编程方式将地图缩放到更加放大的状态(您可以在缩放因子附近使用以获得它,以便注释不会重叠)后你以地图为中心。 – aksh1t

+0

嗨@ aksh1t我考虑过这个,但我不想惹恼用户选择的缩放级别。另外,我的一些注释可以非常接近,所以这在某些情况下可能不起作用。 – Turnip

+0

这很有道理。下面是我在查看其他一些mapview方法时得到的另一个想法:尝试用您的注释调用[showAnnotations:](https://developer.apple.com/documentation/mapkit/mkmapview/1452309-showannotations)方法,以及然后执行'selectAnnotation'。 (我不知道showAnnotation方法是否更改缩放级别;它可能确实会改变缩放级别)。 – aksh1t

回答

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