-
Notifications
You must be signed in to change notification settings - Fork 481
/
ViewController.swift
180 lines (146 loc) · 5.71 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
//
// ViewController.swift
// Displaying Directions on The Map
//
// Created by Vandad Nahavandipoor on 7/8/14.
// Copyright (c) 2014 Pixolity Ltd. All rights reserved.
//
// These example codes are written for O'Reilly's iOS 8 Swift Programming Cookbook
// If you use these solutions in your apps, you can give attribution to
// Vandad Nahavandipoor for his work. Feel free to visit my blog
// at http://vandadnp.wordpress.com for daily tips and tricks in Swift
// and Objective-C and various other programming languages.
//
// You can purchase "iOS 8 Swift Programming Cookbook" from
// the following URL:
// http://shop.oreilly.com/product/0636920034254.do
//
// If you have any questions, you can contact me directly
// at vandad.np@gmail.com
// Similarly, if you find an error in these sample codes, simply
// report them to O'Reilly at the following URL:
// http://www.oreilly.com/catalog/errata.csp?isbn=0636920034254
import UIKit
import MapKit
class ViewController: UIViewController,
MKMapViewDelegate, CLLocationManagerDelegate {
var mapView: MKMapView!
var locationManager: CLLocationManager?
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
mapView = MKMapView()
}
/* Set up the map and add it to our view */
override func viewDidLoad() {
super.viewDidLoad()
mapView.mapType = .Standard
mapView.frame = view.frame
mapView.delegate = self
view.addSubview(mapView)
}
func provideDirections(){
let destination = "Godsgatan, Norrköping, Sweden"
CLGeocoder().geocodeAddressString(destination,
completionHandler: {placemarks, error in
guard let placemarks = placemarks else{
/* Handle the error here perhaps by displaying an alert */
return
}
let request = MKDirectionsRequest()
request.source = MKMapItem.mapItemForCurrentLocation()
/* Convert the CoreLocation destination
placemark to a MapKit placemark */
let placemark = placemarks[0]
let destinationCoordinates =
placemark.location.coordinate
/* Get the placemark of the destination address */
let destination = MKPlacemark(coordinate:
destinationCoordinates,
addressDictionary: nil)
request.destination = MKMapItem(placemark: destination)
/* Set the transportation method to automobile */
request.transportType = .Automobile
/* Get the directions */
let directions = MKDirections(request: request)
directions.calculateDirectionsWithCompletionHandler{
response, error in
guard let response = response else {
//handle the error here
return
}
/* You can manually parse the response, but in
here we will take a shortcut and use the Maps app
to display our source and
destination. We didn't have to make this API call at all,
as we already had the map items before, but this is to
demonstrate that the directions response contains more
information than just the source and the destination. */
/* Display the directions on the Maps app */
let launchOptions = [
MKLaunchOptionsDirectionsModeKey:
MKLaunchOptionsDirectionsModeDriving]
MKMapItem.openMapsWithItems(
[response.source, response.destination],
launchOptions: launchOptions)
}
})
}
func locationManager(manager: CLLocationManager,
didChangeAuthorizationStatus status: CLAuthorizationStatus){
print("The authorization status of location " +
"services is changed to: ", appendNewline: false)
switch CLLocationManager.authorizationStatus(){
case .Denied:
print("Denied")
case .NotDetermined:
print("Not determined")
case .Restricted:
print("Restricted")
default:
print("Authorized")
provideDirections()
}
}
func displayAlertWithTitle(title: String, message: String){
let controller = UIAlertController(title: title,
message: message,
preferredStyle: .Alert)
controller.addAction(UIAlertAction(title: "OK",
style: .Default,
handler: nil))
presentViewController(controller, animated: true, completion: nil)
}
/* Add the pin to the map and center the map around the pin */
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
/* Are location services available on this device? */
if CLLocationManager.locationServicesEnabled(){
/* Do we have authorization to access location services? */
switch CLLocationManager.authorizationStatus(){
case .Denied:
/* No */
displayAlertWithTitle("Not Determined",
message: "Location services are not allowed for this app")
case .NotDetermined:
/* We don't know yet, we have to ask */
locationManager = CLLocationManager()
if let manager = self.locationManager{
manager.delegate = self
manager.requestWhenInUseAuthorization()
}
case .Restricted:
/* Restrictions have been applied, we have no access
to location services */
displayAlertWithTitle("Restricted",
message: "Location services are not allowed for this app")
default:
provideDirections()
}
} else {
/* Location services are not enabled.
Take appropriate action: for instance, prompt the
user to enable the location services */
print("Location services are not enabled")
}
}
}