10
enum AnnotationActionType{
12
case AddNumberAnnotations
13
case AnimateAnnotations
14
case QueryVisibleAnnotations
15
case CenterSelectedAnnotations
16
case AddVisibleAreaPolyline
20
class AnnotationsViewController: UIViewController {
22
var nbMapView: NGLMapView! {
24
oldValue?.removeFromSuperview()
25
if let mapView = nbMapView {
26
view.insertSubview(mapView, at: 0)
27
mapView.delegate = self
34
AnnotationActionType.AddAnnotations,
35
AnnotationActionType.AddNumberAnnotations,
36
AnnotationActionType.AnimateAnnotations,
37
AnnotationActionType.QueryVisibleAnnotations,
38
AnnotationActionType.CenterSelectedAnnotations,
39
AnnotationActionType.AddVisibleAreaPolyline,
43
var points : Array = [CLLocationCoordinate2D]()
45
override func viewDidLoad() {
47
nbMapView = NGLMapView(frame:self.view.bounds)
48
button = UIButton(type: .system)
49
button.setTitle("Settings", for: .normal)
50
button.addTarget(self, action: #selector(showSetings), for: .touchUpInside)
51
button.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
52
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
55
@objc func showSetings() {
56
let tableViewController = UITableViewController(style: .plain)
57
tableViewController.tableView.delegate = self
58
tableViewController.tableView.dataSource = self
59
tableViewController.title = "Camera Settings"
60
self.present(tableViewController, animated: true)
63
func performeSettings(type: AnnotationActionType) {
65
case AnnotationActionType.AddAnnotations :
66
addAnnotations(count: 100)
68
case AnnotationActionType.AddNumberAnnotations :
69
addAnnotations(count: 1000)
71
case .AnimateAnnotations:
74
case .QueryVisibleAnnotations:
75
queryVisibleAnnotations()
77
case .CenterSelectedAnnotations:
78
centerSelectedAnnotation()
80
case .AddVisibleAreaPolyline:
81
addVisibleAreaPolyline()
87
func addAnnotations(count: Int32) {
89
if let annotations = self.nbMapView.annotations {
90
self.nbMapView.removeAnnotations(annotations)
93
DispatchQueue.global(qos: .default).async {
94
if let featuresData = try? Data(contentsOf: Bundle.main.url(forResource: "points", withExtension: "geojson")!),
95
let features = try? JSONSerialization.jsonObject(with: featuresData, options: []) as? [String: Any] {
97
var annotations = [NGLPointAnnotation]()
99
if let featureList = features["features"] as? [[String: Any]] {
100
for feature in featureList {
101
if let coordinates = feature["geometry"] as? [String: Any],
102
let coordinateList = coordinates["coordinates"] as? [Double],
103
let title = feature["properties"] as? [String: Any] {
105
let coordinate = CLLocationCoordinate2D(latitude: coordinateList[1], longitude: coordinateList[0])
106
let annotation = NGLPointAnnotation()
107
annotation.coordinate = coordinate
108
annotation.title = title["NAME"] as? String
110
annotations.append(annotation)
112
if annotations.count == count {
118
DispatchQueue.main.async {
119
self.nbMapView.addAnnotations(annotations)
120
self.nbMapView.showAnnotations(annotations, animated: true)
128
func animateAnnotations () {
129
let annot = NGLPointAnnotation()
130
annot.coordinate = self.nbMapView.centerCoordinate
131
self.nbMapView.addAnnotation(annot)
133
let point = CGPoint(x: self.view.frame.origin.x + 200, y: self.view.frame.midY)
134
let coord = self.nbMapView.convert(point, toCoordinateFrom: self.view)
136
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
137
UIView.animate(withDuration: 1) {
138
annot.coordinate = coord
144
func queryVisibleAnnotations() {
145
let visibleAnnotationCount = NSNumber(value: self.nbMapView.visibleAnnotations?.count ?? 0)
148
if visibleAnnotationCount.intValue == 1 {
149
message = "There is \(visibleAnnotationCount) visible annotation."
151
message = "There are \(visibleAnnotationCount) visible annotations."
154
let alertController = UIAlertController(title: "Visible Annotations", message: message, preferredStyle: .alert)
155
alertController.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
156
self.present(alertController, animated: true, completion: nil)
160
func centerSelectedAnnotation() {
162
if let annotation = self.nbMapView.selectedAnnotations.first {
163
let point = self.nbMapView.convert(annotation.coordinate, toPointTo: self.nbMapView)
164
let center = self.nbMapView.convert(point, toCoordinateFrom: self.nbMapView)
165
self.nbMapView.setCenter(center, animated: true)
169
func addVisibleAreaPolyline() {
170
let constrainedRect = self.nbMapView.bounds.inset(by: self.nbMapView.contentInset)
172
var lineCoords = [CLLocationCoordinate2D]()
174
lineCoords.append(self.nbMapView.convert(CGPoint(x: constrainedRect.minX, y: constrainedRect.minY), toCoordinateFrom: self.nbMapView))
175
lineCoords.append(self.nbMapView.convert(CGPoint(x: constrainedRect.maxX, y: constrainedRect.minY), toCoordinateFrom: self.nbMapView))
176
lineCoords.append(self.nbMapView.convert(CGPoint(x: constrainedRect.maxX, y: constrainedRect.maxY), toCoordinateFrom: self.nbMapView))
177
lineCoords.append(self.nbMapView.convert(CGPoint(x: constrainedRect.minX, y: constrainedRect.maxY), toCoordinateFrom: self.nbMapView))
178
lineCoords.append(lineCoords[0])
180
let line = NGLPolyline(coordinates: &lineCoords, count: UInt(lineCoords.count))
181
self.nbMapView.addAnnotation(line)
189
extension AnnotationsViewController: NGLMapViewDelegate {
190
func mapView(_ mapView: NGLMapView, didFinishLoading style: NGLStyle){
191
nbMapView.setCenter(CLLocationCoordinate2DMake(38.87031006108791, -77.00896639534831), zoomLevel: 10, animated: true)
195
func mapView(_ mapView: NGLMapView, didSelect annotation: NGLAnnotation) {
196
let point = self.nbMapView.convert(annotation.coordinate, toPointTo: self.nbMapView)
197
let center = self.nbMapView.convert(point, toCoordinateFrom: self.nbMapView)
198
self.nbMapView.setCenter(center, zoomLevel: 14, animated: true)
201
func mapView(_ mapView: NGLMapView, annotationCanShowCallout annotation: NGLAnnotation) -> Bool {
206
extension AnnotationsViewController: UITableViewDelegate, UITableViewDataSource {
208
func settingsTitlesForRaw(index: Int) -> String {
209
let type = typeList[index]
211
case AnnotationActionType.AddAnnotations :
212
return "Add 100 Annotations"
213
case AnnotationActionType.AddNumberAnnotations :
214
return "Add 1000 Annotations"
215
case AnnotationActionType.AnimateAnnotations :
216
return "Animate Annotations"
217
case AnnotationActionType.QueryVisibleAnnotations :
218
return "Query Visible Annotations"
219
case AnnotationActionType.CenterSelectedAnnotations :
220
return "Center Selected Annotations"
221
case AnnotationActionType.AddVisibleAreaPolyline :
222
return "Add Visible Area Polyline"
225
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
226
return typeList.count
229
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
230
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
231
cell.textLabel?.text = settingsTitlesForRaw(index: indexPath.row)
235
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
236
tableView.isHidden = true
237
let type = typeList[indexPath.row]
238
dismissSettings(type: type)
241
func dismissSettings(type: AnnotationActionType) {
242
dismiss(animated: true)
243
performeSettings(type: type)