MapView的cluster
2 min readJun 20, 2018
iOS11在地圖的圖標上有改變及新增一些API,像是這個cluster就是可以讓地圖上如果annotation之間的空間不夠容納,會先合在一起,並顯示數量。
以下為原本的地圖,標記會全部顯示出來
在class裡符合MKMapViewDelegate協議,加入此方法:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier, for: annotation)annotationView.clusteringIdentifier = "identifier"return annotationView}
效果是這樣:
但是因為目前沒有什麼時間深入去瞭解,所以只是大概介紹,其他的客製化可以看一下Apple的文檔及下載範例https://developer.apple.com/documentation/mapkit/mkannotationview/decluttering_a_map_with_mapkit_annotation_clustering#see-also
這文檔寫介紹了更多客製化的屬性。