/
ViewController.swift
127 lines (99 loc) · 3.34 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
//
// ViewController.swift
// MiniMap
//
// Created by Adrian Schoenig on 7/7/17.
// Copyright © 2017 SkedGo Pty Ltd. All rights reserved.
//
import Cocoa
import MapKit
import TripKit
class ViewController: NSViewController {
@IBOutlet weak var mapView: MKMapView!
var from: MKAnnotation? = nil
var to: MKAnnotation? = nil
let router = TKBuzzRouter()
override func viewDidLoad() {
super.viewDidLoad()
let presser = NSPressGestureRecognizer(target: self, action: #selector(pressTriggered))
presser.minimumPressDuration = 1
mapView.addGestureRecognizer(presser)
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
@objc
func pressTriggered(_ recognizer: NSPressGestureRecognizer) {
guard recognizer.state == .began else { return }
let isFrom = from == nil
let point: NSPoint = recognizer.location(in: mapView)
let coordinate = mapView.convert(point, toCoordinateFrom: mapView)
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotation.title = isFrom ? "Start" : "End"
if isFrom {
if let oldFrom = from {
mapView.removeAnnotation(oldFrom)
}
from = annotation
} else {
if let oldTo = to {
mapView.removeAnnotation(oldTo)
}
to = annotation
}
mapView.addAnnotation(annotation)
route()
}
func route() {
guard let from = from, let to = to else { return }
router.modeIdentifiers = [
SVKTransportModeIdentifierRegularPublicTransport
]
let request = TripRequest.insert(from: from, to: to, for: nil, timeType: .leaveASAP, into: TripKit.shared.tripKitContext)
router.fetchTrips(for: request, success: { request, modes in
guard let trip = request.trips.first else {
print("Nothing found for \(modes)")
return
}
for segment in trip.segments() {
guard segment.hasVisibility(.onMap), let shapes = segment.shapes() else { continue }
self.mapView.addAnnotation(segment)
for shape in shapes {
guard let polyline = STKRoutePolyline(for: shape) else { continue }
self.mapView.add(polyline)
}
}
}, failure: { error, modes in
print("Error \(error) for \(modes)")
})
}
}
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let pinView: MKPinAnnotationView
if let reused = mapView.dequeueReusableAnnotationView(withIdentifier: "pinView") as? MKPinAnnotationView {
reused.annotation = annotation
pinView = reused
} else {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pinView")
}
pinView.animatesDrop = true
pinView.isDraggable = true
pinView.pinTintColor = annotation.title! == "Start" ? .green : .red
pinView.canShowCallout = true
return pinView
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if let polyline = overlay as? STKRoutePolyline {
let renderer = MKPolylineRenderer(polyline: polyline)
renderer.lineWidth = 10
renderer.strokeColor = polyline.route.routeColor
return renderer
} else {
return MKOverlayRenderer(overlay: overlay)
}
}
}