2017-09-20 63 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的對象。