-
Notifications
You must be signed in to change notification settings - Fork 23
/
Map.swift
executable file
·212 lines (174 loc) · 6.95 KB
/
Map.swift
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//
// Map.swift
// Focals
//
// Created by Caspar Wylie on 06/08/2016.
// Copyright © 2016 Caspar Wylie. All rights reserved.
//
import Foundation
import MapKit
import Darwin
import SceneKit
/*
MAP COMPONENT
*/
@objc protocol mapActionDelegate {
@objc optional func renderTempFocalFromMap(_ mapTapCoord: CLLocationCoordinate2D);
}
class Map: NSObject, MKMapViewDelegate{
var mapView = MKMapView();
var mapActionDelegate: mapActionDelegate?;
var tapMapToPost = false;
var tempPin: MKPointAnnotation!;
var tapRec: UITapGestureRecognizer!;
var misc = Misc();
var scene: Scene!;
var currTempLocation: CLLocation!;
var location = Location();
//MARK: setup map
func renderMap(_ view: UIView){
mapView.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height);
mapView.mapType = MKMapType.standard;
mapView.isZoomEnabled = false;
mapView.isScrollEnabled = false;
//mapView.isUserInteractionEnabled = false;
mapView.delegate = self;
mapView.tag = 3;
mapView.showsUserLocation = true;
addFocalMapTapRecognizer();
view.addSubview(mapView);
}
//MARK: view region setting
func centerToLocationRegion(_ location: CLLocation) -> MKCoordinateRegion{
let radiusArea: Double = 80;
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
radiusArea * 2.0, radiusArea * 2.0);
return coordinateRegion;
}
//MARK: add focal map tap
func addFocalMapTapRecognizer(){
tapRec = UITapGestureRecognizer(target: self, action: #selector(wrapTappedMap));
tapRec.numberOfTapsRequired = 1;
mapView.addGestureRecognizer(tapRec);
}
@objc func wrapTappedMap(_ touch: UITapGestureRecognizer){
let tapPoint = touch.location(in: mapView);
let tapCoords = mapView.convert(tapPoint, toCoordinateFrom: mapView);
if(self.tapMapToPost == true){
mapActionDelegate?.renderTempFocalFromMap!(tapCoords);
}else{
//get focalinfo
}
}
func cancelTempFocal(){
if(tempPin != nil){
mapView.removeAnnotation(tempPin);
tapRec.isEnabled = false;
tempPin = nil;
}
}
//MARK: update pins that represent focal
var pcount = 0;
func updateSinglePin(_ coord: CLLocation, temp: Bool){
let CLLCoordType = CLLocationCoordinate2D(latitude: coord.coordinate.latitude,
longitude: coord.coordinate.longitude);
if(temp == true){
if(tempPin != nil){
mapView.removeAnnotation(tempPin);
}
tempPin = MKPointAnnotation();
tempPin.title = "temp";
tempPin?.coordinate = CLLCoordType;
mapView.addAnnotation(tempPin);
currTempLocation = coord;
}else{
let pin = MKPointAnnotation();
pin.title = String(pcount);
pcount += 1;
pin.coordinate = CLLCoordType;
mapView.addAnnotation(pin);
}
}
func updatePins(_ coords: [CLLocation]){
mapView.removeAnnotations(mapView.annotations);
for coord in coords{
updateSinglePin(coord, temp: false);
}
if(tempPin != nil){
updateSinglePin(currTempLocation, temp: true);
}
}
//MARK: get map view snap shot for openCV wrapper
func getMapAsIMG( _ completion: @escaping (UIImage)->() ){
var finalImage = UIImage();
let imageOptions = MKMapSnapshotOptions();
imageOptions.region = mapView.region;
imageOptions.size = mapView.frame.size;
imageOptions.showsBuildings = true;
imageOptions.showsPointsOfInterest = false;
let imgMap = MKMapSnapshotter(options: imageOptions);
imgMap.start(completionHandler: { (imageObj: MKMapSnapshot?, Error) -> Void in
if(Error != nil){
print("\(Error)");
}else{
finalImage = imageObj!.image;
}
completion(finalImage);
});
}
//MARK: convert single map point to PX
func convertMapPointToPX(_ mapPoint: MKMapPoint) -> (Double,Double){
let mapRect: MKMapRect = mapView.visibleMapRect;
let tlMapPoint: MKMapPoint = MKMapPointMake( MKMapRectGetMinX(mapRect), mapRect.origin.y);
let mapRectHeight = MKMapRectGetHeight(mapRect);
let mapRectWidth = MKMapRectGetWidth(mapRect);
let XinRectAsPercent = (mapPoint.x - tlMapPoint.x)/mapRectWidth;
let YinRectAsPercent = (mapPoint.y - tlMapPoint.y)/mapRectHeight;
return (XinRectAsPercent, YinRectAsPercent);
}
//MARK: check focals apart
func focalIsolated(mapPoint: MKMapPoint, mapPoints: [MKMapPoint]) -> Bool{
var isolated = true;
for existingPoint in mapPoints{
let distanceBetweenPoints = location.getDistanceBetweenTwo2dPoints(mapPoint.x, point1Y: mapPoint.y, point2X: existingPoint.x, point2Y: existingPoint.y);
if(distanceBetweenPoints < 40){
isolated = false;
break;
}
}
return isolated;
}
//MARK: get all map points as px in preparation for openCV wrapper
func collectPXfromMapPoints(_ mapPoints: [MKMapPoint], currMapPoint: MKMapPoint)
-> (focalValsPX: [(Double,Double)], currPointPX:[Double], pxLength: Int){
var pixelsXY: [(Double,Double)] = [];
let resultsCurrPointXY_T = convertMapPointToPX(currMapPoint);
let resultsCurrPointXY = [resultsCurrPointXY_T.0,resultsCurrPointXY_T.1];
for mapPoint in mapPoints{
let resultsPX = convertMapPointToPX(mapPoint);
pixelsXY.append(resultsPX);
}
return (focalValsPX: pixelsXY, currPointPX: resultsCurrPointXY, pxLength: mapPoints.count);
}
//MARK: convert coordinate data to 2D map points
func getCoordsAsMapPoints(_ coords: [CLLocation]) -> [MKMapPoint]{
var mapPoints: [MKMapPoint] = [];
for coord in coords{
mapPoints.append(MKMapPointForCoordinate(coord.coordinate));
}
return mapPoints;
}
//MARK: update pins delegate call, render, etc
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation{
return nil;
}else{
let pinIdent = "Pin";
var pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: pinIdent);
if(annotation.title! == "temp"){
pinView.pinTintColor = UIColor(red: 0.6275, green: 0, blue: 0, alpha: 1.0);
}
return pinView;
}
}
}