-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewController.swift
390 lines (259 loc) · 13 KB
/
ViewController.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
//
// ViewController.swift
// PizzaHunters
//
// Created by Mohammad AZam on 7/14/15.
// Copyright (c) 2015 Mohammad AZam. All rights reserved.
//
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate, UITextFieldDelegate {
@IBOutlet weak var mapView :MKMapView?
@IBOutlet weak var addressTextField :UITextField?
var locationManager :CLLocationManager = CLLocationManager()
var currentLocation :CLLocation?
var geocoder :CLGeocoder = CLGeocoder()
var cityList : [String] = []
public let coordinates: [CLLocationCoordinate2D]?
/// The encoded polyline
public let encodedPolyline: String
/// The array of levels (nil if cannot be decoded, or is not provided)
public let levels: [UInt32]?
/// The encoded levels (nil if cannot be encoded, or is not provided)
public let encodedLevels: String?
/// The array of location (computed from coordinates)
public var locations: [CLLocation]? {
return self.coordinates.map(toLocations)
}
override func viewDidLoad() {
super.viewDidLoad()
setup()
}
func setup() {
self.locationManager.delegate = self
self.locationManager.distanceFilter = kCLHeadingFilterNone
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestAlwaysAuthorization()
self.locationManager.startUpdatingLocation()
self.mapView!.showsUserLocation = true
// dropFavoriteAnnotation()
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
let address = self.addressTextField?.text
openDirections(address)
return textField.resignFirstResponder()
}
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer! {
let renderer = MKPolylineRenderer(overlay: overlay)
renderer.strokeColor = UIColor.blueColor()
renderer.lineWidth = 5.0
return renderer
}
func openDirections(address :String?) {
self.geocoder.geocodeAddressString(address!, completionHandler: { (placemarks :[CLPlacemark]?, error :NSError?) -> Void in
let placemark = placemarks![0] as? CLPlacemark
let destinationPlacemark = MKPlacemark(coordinate: placemark!.location!.coordinate, addressDictionary: placemark?.addressDictionary as? [String:NSObject])
let startingMapItem = MKMapItem.mapItemForCurrentLocation()
let destinationMapItem = MKMapItem(placemark: destinationPlacemark)
let directionsRequest = MKDirectionsRequest()
directionsRequest.transportType = .Automobile
directionsRequest.source = startingMapItem
directionsRequest.destination = destinationMapItem
let directions = MKDirections(request: directionsRequest)
directions.calculateDirectionsWithCompletionHandler({ (response :MKDirectionsResponse?, error :NSError?) -> Void in
let route = response!.routes[0] as? MKRoute
if route!.steps.count > 0 {
for step in route!.steps {
print(step.instructions)
}
}
self.mapView!.addOverlay((route?.polyline)!, level: MKOverlayLevel.AboveRoads)
var cities :Int = 0
for var pointCount = 0; pointCount <= route?.polyline.pointCount; ++pointCount{
print("Test")
self.cityList[cities] = self.searchCity((route?.polyline.coordinate.latitude)!, longitude: (route?.polyline.coordinate.longitude)!)
cities++
}
})
// let mapItems = [destinationMapItem]
//
// let launchOptions = [MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving]
//
// let success = MKMapItem.openMapsWithItems(mapItems, launchOptions: launchOptions)
})
}
public func decodePolyline(encodedPolyline :String, precision: Double = 1e5)->[CLLocationCoordinate2D]{
let data = encodedPolyline.dataUsingEncoding(NSUTF8StringEncoding)!
let byteArray = unsafeBitCast(data.bytes, UnsafePointer<Int8>.self)
let length = Int(data.length)
var position = Int(0)
var decodedCoordinates = [CLLocationCoordinate2D]()
var lat = 0.0
var lon = 0.0
while position < length{
do {
let resultingLat = try decodeSingleCoordinate(byteArray: byteArray, length: length, position: &position, precision: precision)
lat += resultingLat
let resultingLon = try decodeSingleCoordinate(byteArray: byteArray, length: length, position: &position, precision: precision)
lon += resultingLon
} catch {
//return nil
print("Error")
}
decodedCoordinates.append(CLLocationCoordinate2D(latitude: lat, longitude: lon))
}
return decodedCoordinates
}
public func decodePolyline(encodedPolyline: String, precision: Double = 1e5) -> [CLLocation]? {
return decodePolyline(encodedPolyline, precision: precision).map(toLocations)
}
private func decodeSingleCoordinate(byteArray byteArray: UnsafePointer<Int8>, length: Int, inout position: Int, precision: Double = 1e5) throws -> Double {
guard position < length else { throw PolylineError.SingleCoordinateDecodingError }
let bitMask = Int8(0x1F)
var coordinate: Int32 = 0
var currentChar: Int8
var componentCounter: Int32 = 0
var component: Int32 = 0
repeat {
currentChar = byteArray[position] - 63
component = Int32(currentChar & bitMask)
coordinate |= (component << (5*componentCounter))
position++
componentCounter++
} while ((currentChar & 0x20) == 0x20) && (position < length) && (componentCounter < 6)
if (componentCounter == 6) && ((currentChar & 0x20) == 0x20) {
throw PolylineError.SingleCoordinateDecodingError
}
if (coordinate & 0x01) == 0x01 {
coordinate = ~(coordinate >> 1)
} else {
coordinate = coordinate >> 1
}
return Double(coordinate) / precision
}
func searchCity(latitude :Double, longitude :Double) -> String{
var city :String = ""
var localGeoCoder :CLGeocoder = CLGeocoder()
let location = CLLocation(latitude: latitude, longitude: longitude)
localGeoCoder.reverseGeocodeLocation(location){
(placemarks, error) -> Void in
let placeArray = placemarks as [CLPlacemark]!
var placeMark: CLPlacemark!
placeMark = placeArray?[0]
print(placeMark.addressDictionary)
let locationName = placeMark.addressDictionary?["Name"] as? String
print(locationName)
city = locationName!
print("testing123")
print(city)
}
print(city)
return city
}
func dropFavoriteAnnotation() {
let googleCoordinate = CLLocationCoordinate2D(latitude: 37.422, longitude: -122.084058)
let favAnnotation = PizzaAnnotation(coordinate: googleCoordinate, title: "Favorite Pizza", subtitle: nil)
self.mapView?.addAnnotation(favAnnotation)
// register to monitor region
let region = CLCircularRegion(center: googleCoordinate, radius: 1000, identifier: "FavoritePizzaRegion")
self.mapView?.addOverlay(MKCircle(centerCoordinate: googleCoordinate, radius: 1000))
region.notifyOnEntry = true
region.notifyOnExit = true
self.locationManager.startMonitoringForRegion(region)
}
func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion) {
print("Region Entered")
}
func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion) {
print("Region Exited")
}
// func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
// if overlay is MKCircle {
// var circleRenderer = MKCircleRenderer(overlay: overlay)
// circleRenderer.lineWidth = 1.0
// circleRenderer.strokeColor = UIColor.purpleColor()
// circleRenderer.fillColor = UIColor.purpleColor().colorWithAlphaComponent(0.4)
// return circleRenderer
// }
// return nil
// }
func locationManager(manager: CLLocationManager, didStartMonitoringForRegion region: CLRegion) {
print("didStartMonitoringForRegion")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation) {
self.currentLocation = userLocation.location
let region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 250, 250)
self.mapView!.setRegion(region, animated: true)
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView! {
if annotation.isKindOfClass(MKUserLocation) {
return nil
}
var annotationView = self.mapView?.dequeueReusableAnnotationViewWithIdentifier("PizzaAnnotationView")
if(annotationView == nil) {
annotationView = PizzaAnnotationView(annotation: annotation, reuseIdentifier: "PizzaAnnotationView")
annotationView?.canShowCallout = true
}
else {
annotationView?.annotation = annotation
}
return annotationView
}
func addAnnotationToMap() {
self.geocoder.reverseGeocodeLocation(self.currentLocation!) { (placemarks :[CLPlacemark]?, error :NSError?) -> Void in
if error != nil { return }
let placemark = placemarks![0] as? CLPlacemark
if let streetName = placemark!.addressDictionary!["Street"] as? String {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let pizzaAnnotation = PizzaAnnotation(coordinate: self.currentLocation!.coordinate, title: "Pizza Hunters", subtitle: streetName)
self.mapView!.addAnnotation(pizzaAnnotation)
})
}
}
// self.geocoder.reverseGeocodeLocation(self.currentLocation!, completionHandler: { (placemarks :[AnyObject]!, error :NSError!) -> Void in
//
// if error != nil { return }
//
// let placemark = placemarks[0] as! CLPlacemark
//
// if let streetName = placemark.addressDictionary!["Street"] as? String {
//
// dispatch_async(dispatch_get_main_queue(), { () -> Void in
//
// let pizzaAnnotation = PizzaAnnotation(coordinate: self.currentLocation!.coordinate, title: "Pizza Hunters", subtitle: streetName)
//
// self.mapView!.addAnnotation(pizzaAnnotation)
//
// })
//
// }
//
// })
}
override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent?) {
if(motion == .MotionShake) {
addAnnotationToMap()
}
}
}
enum PolylineError: ErrorType {
case SingleCoordinateDecodingError
case ChunkExtractingError
}
private func toCoordinates(locations: [CLLocation]) -> [CLLocationCoordinate2D] {
return locations.map {location in location.coordinate}
}
private func toLocations(coordinates: [CLLocationCoordinate2D]) -> [CLLocation] {
return coordinates.map { coordinate in
CLLocation(latitude:coordinate.latitude, longitude:coordinate.longitude)
}
}
private func isSeparator(value: Int32) -> Bool {
return (value - 63) & 0x20 != 0x20
}
private typealias IntegerCoordinates = (latitude: Int, longitude: Int)