Skip to content

Commit

Permalink
Drop two pins in sample project to get a route
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Schoenig committed Aug 11, 2017
1 parent cdc3eea commit 8f5a64d
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 9 deletions.
@@ -0,0 +1,93 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0900"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3A1498D51F0FD33F0061A0D0"
BuildableName = "MiniMap.app"
BlueprintName = "MiniMap"
ReferencedContainer = "container:MiniMap.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3A1498D51F0FD33F0061A0D0"
BuildableName = "MiniMap.app"
BlueprintName = "MiniMap"
ReferencedContainer = "container:MiniMap.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3A1498D51F0FD33F0061A0D0"
BuildableName = "MiniMap.app"
BlueprintName = "MiniMap"
ReferencedContainer = "container:MiniMap.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3A1498D51F0FD33F0061A0D0"
BuildableName = "MiniMap.app"
BlueprintName = "MiniMap"
ReferencedContainer = "container:MiniMap.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
7 changes: 3 additions & 4 deletions Examples/MiniMap/MiniMap/AppDelegate.swift
Expand Up @@ -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 = ...
Expand All @@ -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()
Expand All @@ -44,7 +44,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}



}

15 changes: 12 additions & 3 deletions Examples/MiniMap/MiniMap/Base.lproj/Main.storyboard
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="12121" systemVersion="16F73" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="13178.6" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="12121"/>
<plugIn identifier="com.apple.MapKitIBPlugin" version="12121"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="13178.6"/>
<plugIn identifier="com.apple.MapKitIBPlugin" version="13178.6"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand Down Expand Up @@ -670,6 +670,9 @@
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="196" y="240" width="480" height="270"/>
<rect key="screenRect" x="0.0" y="0.0" width="1680" height="1027"/>
<connections>
<outlet property="delegate" destination="B8D-0N-5wS" id="s4E-HH-vlk"/>
</connections>
</window>
<connections>
<segue destination="XfG-lQ-9wD" kind="relationship" relationship="window.shadowedContentViewController" id="cq2-FE-JQM"/>
Expand All @@ -689,6 +692,9 @@
<subviews>
<mapView mapType="standard" showsScale="YES" translatesAutoresizingMaskIntoConstraints="NO" id="b9O-81-Coo">
<rect key="frame" x="0.0" y="0.0" width="480" height="270"/>
<connections>
<outlet property="delegate" destination="XfG-lQ-9wD" id="LbU-Df-sbw"/>
</connections>
</mapView>
</subviews>
<constraints>
Expand All @@ -698,6 +704,9 @@
<constraint firstItem="b9O-81-Coo" firstAttribute="top" secondItem="m2S-Jp-Qdl" secondAttribute="top" id="cXz-Sz-lBb"/>
</constraints>
</view>
<connections>
<outlet property="mapView" destination="b9O-81-Coo" id="xwv-OE-PhT"/>
</connections>
</viewController>
<customObject id="rPt-NT-nkU" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
Expand Down
102 changes: 101 additions & 1 deletion Examples/MiniMap/MiniMap/ViewController.swift
Expand Up @@ -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? {
Expand All @@ -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)
}
}

}
2 changes: 1 addition & 1 deletion Project/TripKit.xcodeproj/project.pbxproj
Expand Up @@ -907,7 +907,7 @@
3AFFAB7E1F06854F00147990 /* STKModeHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = STKModeHelper.swift; sourceTree = "<group>"; };
3AFFAB7F1F06854F00147990 /* STKRoutePolyline.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = STKRoutePolyline.h; sourceTree = "<group>"; };
3AFFAB801F06854F00147990 /* STKRoutePolyline.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = STKRoutePolyline.m; sourceTree = "<group>"; };
3AFFAB811F06854F00147990 /* STKRoutePolyline.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = STKRoutePolyline.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
3AFFAB811F06854F00147990 /* STKRoutePolyline.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = STKRoutePolyline.swift; sourceTree = "<group>"; };
3AFFAB821F06854F00147990 /* STKTransportKit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = STKTransportKit.h; sourceTree = "<group>"; };
3AFFAB831F06854F00147990 /* STKTripAndSegments.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = STKTripAndSegments.swift; sourceTree = "<group>"; };
3AFFAB841F06854F00147990 /* STKVehicleType.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = STKVehicleType.h; sourceTree = "<group>"; };
Expand Down

0 comments on commit 8f5a64d

Please sign in to comment.