diff --git a/Examples/MiniMap/MiniMap.xcodeproj/xcshareddata/xcschemes/MiniMap.xcscheme b/Examples/MiniMap/MiniMap.xcodeproj/xcshareddata/xcschemes/MiniMap.xcscheme new file mode 100644 index 000000000..0fb7d0425 --- /dev/null +++ b/Examples/MiniMap/MiniMap.xcodeproj/xcshareddata/xcschemes/MiniMap.xcscheme @@ -0,0 +1,93 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Examples/MiniMap/MiniMap/AppDelegate.swift b/Examples/MiniMap/MiniMap/AppDelegate.swift index 735831f22..8ad57c200 100644 --- a/Examples/MiniMap/MiniMap/AppDelegate.swift +++ b/Examples/MiniMap/MiniMap/AppDelegate.swift @@ -15,7 +15,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { func applicationDidFinishLaunching(_ aNotification: Notification) { - TripKit.apiKey = "MY_API_KEY" + TripKit.apiKey = ProcessInfo.processInfo.environment["TRIPGO_API_KEY"] ?? "MY_API_KEY" TripKit.prepareForNewSession() // let start: MKAnnotation = ... @@ -24,7 +24,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { // let request = TripRequest.insert( // from: start, to: end, // for: nil, timeType: .leaveASAP, -// into: TKTripKit.sharedInstance().tripKitContext +// into: TripKit.shared.tripKitContext // ) // // let router = TKBuzzRouter() @@ -44,7 +44,6 @@ class AppDelegate: NSObject, NSApplicationDelegate { func applicationWillTerminate(_ aNotification: Notification) { // Insert code here to tear down your application } - - + } diff --git a/Examples/MiniMap/MiniMap/Base.lproj/Main.storyboard b/Examples/MiniMap/MiniMap/Base.lproj/Main.storyboard index d9905c742..dc12f6702 100644 --- a/Examples/MiniMap/MiniMap/Base.lproj/Main.storyboard +++ b/Examples/MiniMap/MiniMap/Base.lproj/Main.storyboard @@ -1,9 +1,9 @@ - + - - + + @@ -670,6 +670,9 @@ + + + @@ -689,6 +692,9 @@ + + + @@ -698,6 +704,9 @@ + + + diff --git a/Examples/MiniMap/MiniMap/ViewController.swift b/Examples/MiniMap/MiniMap/ViewController.swift index 3ff7a0365..ed9c97471 100644 --- a/Examples/MiniMap/MiniMap/ViewController.swift +++ b/Examples/MiniMap/MiniMap/ViewController.swift @@ -7,13 +7,25 @@ // 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() - // Do any additional setup after loading the view. + let presser = NSPressGestureRecognizer(target: self, action: #selector(pressTriggered)) + presser.minimumPressDuration = 1 + mapView.addGestureRecognizer(presser) } override var representedObject: Any? { @@ -22,6 +34,94 @@ class ViewController: NSViewController { } } + @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) + } + } + +} diff --git a/Project/TripKit.xcodeproj/project.pbxproj b/Project/TripKit.xcodeproj/project.pbxproj index 4e960bacb..95c001c7a 100644 --- a/Project/TripKit.xcodeproj/project.pbxproj +++ b/Project/TripKit.xcodeproj/project.pbxproj @@ -907,7 +907,7 @@ 3AFFAB7E1F06854F00147990 /* STKModeHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = STKModeHelper.swift; sourceTree = ""; }; 3AFFAB7F1F06854F00147990 /* STKRoutePolyline.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = STKRoutePolyline.h; sourceTree = ""; }; 3AFFAB801F06854F00147990 /* STKRoutePolyline.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = STKRoutePolyline.m; sourceTree = ""; }; - 3AFFAB811F06854F00147990 /* STKRoutePolyline.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = STKRoutePolyline.swift; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; + 3AFFAB811F06854F00147990 /* STKRoutePolyline.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = STKRoutePolyline.swift; sourceTree = ""; }; 3AFFAB821F06854F00147990 /* STKTransportKit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = STKTransportKit.h; sourceTree = ""; }; 3AFFAB831F06854F00147990 /* STKTripAndSegments.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = STKTripAndSegments.swift; sourceTree = ""; }; 3AFFAB841F06854F00147990 /* STKVehicleType.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = STKVehicleType.h; sourceTree = ""; };