-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClusterMapDesignable.swift
More file actions
85 lines (69 loc) · 3.11 KB
/
ClusterMapDesignable.swift
File metadata and controls
85 lines (69 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// ClusterDesignable.swift
// QuadTree
//
// Created by Aaron Dean Bikis on 3/3/17.
// Copyright © 2017 7apps. All rights reserved.
//
import UIKit
import MapKit
/**
To build a quad tree first conform any data structure to *ClusterMapLocation*.
*Before building the QuadTree*: Instanciate an property instance of QuadTreeCoordinator, MKMapView and conform the presenting class with this protocol's methods.
*On ViewDidLoad or elsewhere* call *buildQuadTree(mapView, locations)*
*To update the QuadTreeRegion* call *quadTreeRegionDidChange(_ animated: Bool)*
*/
public protocol ClusterMapDesignable {
var quadTreeCoordinator: CoordinateQuadTree! { get set }
var mapView: MKMapView! { get set }
/**
parameters:
- mapview : MKMapView
- locations: Array<ClusterMapLoctaion>
*/
func buildQuadTree(withMapView mapView:MKMapView, locations:[ClusterMapLocation])
/**
Call this to update the quadTree region after the mapView region changed.
It observes the mapView.visibleMapRect
*/
func quadTreeRegionDidChange()
}
public extension ClusterMapDesignable {
func buildQuadTree(withMapView mapView:MKMapView, locations:[ClusterMapLocation]) {
guard quadTreeCoordinator != nil else { fatalError(ClusterMapErrors.quadTreeCoordinatorNotIntaciated.description) }
quadTreeCoordinator.mapView = mapView
let count = Int32(locations.count)
quadTreeCoordinator.createNodeData(withCount: count)
for i in 0..<locations.count {
let node = quadTreeCoordinator.buildNode(withPointwithLat: locations[i].latitude,
withLong: locations[i].longitude,
andID: Int32(locations[i].id))
quadTreeCoordinator.addNode(to: node, at: Int32(i))
}
quadTreeCoordinator.createRoot(withCount: count)
}
func quadTreeRegionDidChange() {
DispatchQueue.global(qos: .background).async {
let scale = Double(self.mapView.bounds.size.width) / self.mapView.visibleMapRect.size.width
guard let annotations = self.quadTreeCoordinator.clusteredAnnotations(within: self.mapView.visibleMapRect, withZoomScale: scale) as? [ClusterAnnotation] else { return }
self.updateMapViewAnnotations(with: annotations)
}
}
func updateMapViewAnnotations(with annotations: [ClusterAnnotation]){
let mapViewAnnot = mapView.annotations.filter({ $0 is ClusterAnnotation }) as! [ClusterAnnotation]
let before = Set(mapViewAnnot)
let after = Set(annotations)
var toKeep = Set(before)
toKeep = toKeep.intersection(after)
var toAdd = Set(after)
toAdd.subtract(toKeep)
var toRemove = Set(before)
toRemove.subtract(after)
DispatchQueue.main.async {
print("adding \(toAdd.count)")
self.mapView.addAnnotations(Array(toAdd))
print("removing \(toRemove.count)")
self.mapView.removeAnnotations(Array(toRemove))
}
}
}