A drop-in map annotation clustering solution, modified from TBAnnotationClustering. This framework powers my iOS app CafeFreelance.
Note: If your app is targeted at iOS 11 or later, we recommend using iOS 11 built-in cluster APIs instead. Check out WWDC 2017 Session 237: What's New in MapKit.
iOS 9 or later, but earlier iOS version may work.
In light of this discussion, we encourage this form:
pod 'NDLAnnotationClustering', :git => 'https://github.com/Nandalu/NDLAnnotationClustering'
Run pod install
. For first time CocoaPods user, run pod init
to generate Podfile template automatically.
github "Nandalu/NDLAnnotationClustering"
Follow instructions on Carthage.
Add as submodule:
git submodule add https://github.com/Nandalu/NDLAnnotationClustering
Then manually add project file to your project, like "Apps with Multiple Xcode Projects" as follows:
- Drag
NDLAnnotationClustering.xcodeproj
into your project. - Project settings - Targets - General - Embedded Binaries: add
NDLAnnotationClustering.frameworkiOS
See more ways on Apple's Technical Note TN2435: Embedding Frameworks In An App.
Final words: For optimizing your app size, add source files (rather than project file) directly to your project.
Init NDLCoordinateQuadTree. Input your data in one-dimensional array [NSDictionary]
format with method below. For each dictionary data, lat
and lng
values are required. Callout would be shown with title
and subtitle
values provided.
- (void)buildTreeWith:(NSArray*)dataDictArray
WorldMinLat:(CLLocationDegrees)minLat
worldMaxLat:(CLLocationDegrees)maxLat
worldMinLng:(CLLocationDegrees)minLng
worldMaxLng:(CLLocationDegrees)maxLng
completion:(void (^)(void))completion;
World: the map region that covers all of your data points. Data points outside the world will not be used.
You may add data later after tree is built:
- (void)addDataDictArray:(NSArray*)dataDictArray
completion:(void (^)(void))completion;
Given map region, all data points within will be clustered, returned as two-dimensional array [[NSDictionary]]
format, in this method:
- (NSArray*)clusteredAnnotationsDataArrayWithinMapRect:(MKMapRect)rect withZoomScale:(double)zoomScale;
Or, with built-in NDLClusterAnnotation
as data model, data points will be returned as one-dimensional array [NDLClusterAnnotation]
format, in this method:
- (NSArray*)clusteredAnnotationsWithinMapRect:(MKMapRect)rect withZoomScale:(double)zoomScale;
The zoomScale should be given with self.mapView.bounds.size.width / self.mapView.visibleMapRect.size.width
.
All source code is licensed under the MIT License. See LICENSE.