2017-10-19 157 views
1

首先,我的代碼完美運行。 我已經很好地設置了mapView.register和我的註釋標記和集羣。Mkmap iOS11羣集在最大變焦後不分割,如何設置?

當我縮小聚焦視圖中預期的註釋融合時, 當我放大時,除了某個點以外,還有相同的好結果。當太多的註釋距離太近時,集羣視圖不會再分成兩個註釋視圖。

因此,我搜索一種方式來設置這個「縮放級別」,即使相互之間真的很接近,也會出現我的兩個註釋。

這裏是我與地圖上的高變焦集羣的觀點: enter image description here

在這裏,如果我變焦,在最大: 好,集羣意見一個一分爲二,但不露4註釋。
enter image description here

我也嘗試將displayPriority設置爲高於我的兩個批註,比羣集視圖更高,但結果仍然相同。 任何想法?

回答

1

您將需要跟蹤地圖的縮放級別,並在跨越您指定的縮放級別時重新加載註釋。

private let maxZoomLevel = 9 
private var previousZoomLevel: Int? 
private var currentZoomLevel: Int? { 
    willSet { 
     self.previousZoomLevel = self.currentZoomLevel 
    } 
    didSet { 
     // if we have crossed the max zoom level, request a refresh 
     // so that all annotations are redrawn with clustering enabled/disabled 
     guard let currentZoomLevel = self.currentZoomLevel else { return } 
     guard let previousZoomLevel = self.previousZoomLevel else { return } 
     var refreshRequired = false 
     if currentZoomLevel > self.maxZoomLevel && previousZoomLevel <= self.maxZoomLevel { 
      refreshRequired = true 
     } 
     if currentZoomLevel <= self.maxZoomLevel && previousZoomLevel > self.maxZoomLevel { 
      refreshRequired = true 
     } 
     if refreshRequired { 
      // remove the annotations and re-add them, eg 
      let annotations = self.mapView.annotations 
      self.mapView.removeAnnotations(annotations) 
      self.mapView.addAnnotations(annotations) 
     } 
    } 
} 

private var shouldCluster: Bool { 
    if let zoomLevel = self.currentZoomLevel, zoomLevel <= maxZoomLevel { 
     return false 
    } 
    return true 
} 

func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) { 
    // https://stackoverflow.com/a/40616239/883413 
    let zoomWidth = mapView.visibleMapRect.size.width 
    let zoomLevel = Int(log2(zoomWidth)) 
    self.currentZoomLevel = zoomLevel 
} 

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    // for me, annotation reuse doesn't work with clustering 
    let annotationView = CustomAnnotationView(annotation: annotation) 
    if self.shouldCluster { 
     annotationView.clusteringIdentifier = "custom-id" 
    } else { 
     annotationView.clusteringIdentifier = nil 
    } 
    return annotationView 
}