TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Feb 2017 |
SwiftSwift Version | 3.0 |
SPMSupports SPM | ✗ |
Maintained by Michael Skiba, Rob Visentin, RZAffirm, Raiz Labs, Alex Rouse, Rightpoint CI.
Map Clustering for Swift
BentoMap is an Swift implementation of quadtrees for map annotation clustering, and storage. It can also allow other 2d coordinate data to conform to a protocol and be added into BentoBox
containers.
For more information, check out the Raizlabs Developer Blog. The Android equivalent, known as “Marker Clustering,” is documented here.
.swift
files in BentoMap/
and BentoMap/Extensions/
and drop them into your project.To see a full implementation of loading data into a map view, check out the example project.
import BentoMap
static var sampleData: QuadTree<Int, MKMapRect, MKMapPoint> {
var samples = QuadTree<Int, MKMapRect, MKMapPoint>(bentoBox: BentoBox(minPoint: MKMapPointForCoordinate(CLLocationCoordinate2D.minCoord), maxPoint: MKMapPointForCoordinate(CLLocationCoordinate2D.maxCoord)), bucketCapacity: 5)
let randomData = (1...5000).map { count in
return QuadTreeNode(originCoordinate: MKMapPointForCoordinate(CLLocationCoordinate2D.randomCoordinate()), content: count)
}
for node in randomData {
samples.insertNode(node)
}
return samples
}
func updateAnnotations(inMapView mapView: MKMapView,
forMapRect root: MKMapRect) {
guard !mapView.frame.isEmpty && !MKMapRectIsEmpty(root) else {
mapView.removeAnnotations(mapView.annotations)
return
}
let zoomScale = Double(mapView.frame.width) / root.size.width
let clusterResults = mapData.clusteredDataWithinMapRect(root,
zoomScale: zoomScale,
cellSize: Double(MapKitViewController.cellSize))
let newAnnotations = clusterResults.map(BaseAnnotation.makeAnnotation)
let oldAnnotations = mapView.annotations.flatMap({ $0 as? BaseAnnotation })
let toRemove = oldAnnotations.filter { annotation in
return !newAnnotations.contains { newAnnotation in
return newAnnotation == annotation
}
}
mapView.removeAnnotations(toRemove)
let toAdd = newAnnotations.filter { annotation in
return !oldAnnotations.contains { oldAnnotation in
return oldAnnotation == annotation
}
}
mapView.addAnnotations(toAdd)
}
Issues and pull requests are welcome! Please ensure that you have the latest SwiftLint installed before committing and that there are no style warnings generated when building.
Contributors are expected to abide by the Contributor Covenant Code of Conduct.
BentoMap is available under the MIT license. See the LICENSE
file for more info.