/
RoutingViewController.swift
89 lines (76 loc) · 3.19 KB
/
RoutingViewController.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
//
// RoutingViewController.swift
// ios-sdk
//
// Created by Matt Smollinger on 8/17/16.
// Copyright © 2016 Mapzen. All rights reserved.
//
import UIKit
import OnTheRoad
import Pelias
protocol RoutingSearchDelegate {
func selected( location: PeliasMapkitAnnotation )
}
class RoutingViewController: UIViewController, RoutingSearchDelegate {
@IBOutlet weak var searchBar: UITextField!
let routeSearchSegueID = "showRouteSearchSegue"
let routeResultEmbedSegueID = "routeResultEmbedSegue"
let routeListSegueId = "routeListSegue"
var routeResultTable : RouteDisplayViewController?
var currentRouteResult: OTRRoutingResult?
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
guard let identifier = segue.identifier else {
return
}
switch identifier {
case routeSearchSegueID:
if let searchVC = segue.destinationViewController as? RoutingSearchVC {
searchVC.delegate = self
}
break
case routeResultEmbedSegueID:
if let resultVC = segue.destinationViewController as? RouteDisplayViewController {
routeResultTable = resultVC
}
break
case routeListSegueId:
guard let routeResult = currentRouteResult else { return }
if let vc = segue.destinationViewController as? RoutingResultTableVC {
vc.routingResult = routeResult
}
default:
break
}
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if LocationManager.sharedManager.isAlwaysAuthorized() || LocationManager.sharedManager.isInUseAuthorized() {
LocationManager.sharedManager.startUpdatingLocation()
return
}
LocationManager.sharedManager.requestWhenInUseAuthorization()
}
func routeTo(point: PeliasMapkitAnnotation) {
let routingController = OTRRoutingController();
routingController.urlQueryComponents.addObject(NSURLQueryItem(name: "apiKey", value: "mapzen-2qQR7SX"))
var startingPoint = OTRRoutingPoint(coordinate: OTRGeoPointMake(40.7444892, -73.9900082), type: .Break)
if let location = LocationManager.sharedManager.currentLocation {
startingPoint = OTRRoutingPoint(coordinate: OTRGeoPointMake(location.coordinate.latitude, location.coordinate.longitude), type: .Break)
}
let endingPoint = OTRRoutingPoint(coordinate: OTRGeoPointMake(point.coordinate.latitude, point.coordinate.longitude), type: .Break)
routingController.requestRouteWithLocations([startingPoint, endingPoint],
costingModel: .Auto,
costingOption: nil,
directionsOptions: ["units" : "miles"]) { (routingResult, token, error) in
print(routingResult?.legs);
print("Error:\(error)")
self.currentRouteResult = routingResult
self.routeResultTable?.show(routingResult!)
}
}
func selected(location: PeliasMapkitAnnotation) {
print("Selected \(location.title)")
searchBar.text = location.title
routeTo(location)
}
}