-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #558 from mapbox/release-v2.0
Merge release-v2.0 to main (as of v2.0.0-beta.5)
- Loading branch information
Showing
69 changed files
with
785 additions
and
1,178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
github "raphaelmor/Polyline" ~> 5.0 | ||
github "mapbox/turf-swift" ~> 1.0 | ||
github "mapbox/turf-swift" "v2.0.0-beta.1" | ||
github "Udumft/SwiftCLI" "carthage-fix" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
binary "https://www.mapbox.com/ios-sdk/Mapbox-iOS-SDK.json" ~> 5.5 | ||
github "AliSoftware/OHHTTPStubs" ~> 9.0 | ||
github "mapbox/mapbox-events-ios" ~> 0.10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
binary "https://www.mapbox.com/ios-sdk/Mapbox-iOS-SDK.json" "5.9.0" | ||
github "AliSoftware/OHHTTPStubs" "9.1.0" | ||
github "Udumft/SwiftCLI" "da19d2a16cd5aa838d8fb7256e28c171bc67dd82" | ||
github "mapbox/mapbox-events-ios" "v0.10.7" | ||
github "mapbox/turf-swift" "v1.2.0" | ||
github "mapbox/mapbox-events-ios" "v0.10.8" | ||
github "mapbox/turf-swift" "v2.0.0-beta.1" | ||
github "raphaelmor/Polyline" "v5.0.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,10 @@ | ||
import UIKit | ||
import SwiftUI | ||
|
||
@UIApplicationMain | ||
class AppDelegate: UIResponder, UIApplicationDelegate { | ||
var window: UIWindow? | ||
|
||
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { | ||
window = UIWindow(frame: UIScreen.main.bounds) | ||
window!.rootViewController = ViewController(nibName: nil, bundle: nil) | ||
window!.makeKeyAndVisible() | ||
|
||
return true | ||
@main | ||
struct TestApp: App { | ||
var body: some Scene { | ||
WindowGroup { | ||
ContentView(vm: DirectionsViewModel()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import Foundation | ||
import SwiftUI | ||
import Combine | ||
import MapboxDirections | ||
|
||
final class DirectionsViewModel: ObservableObject { | ||
private let distanceFormatter: LengthFormatter = .init() | ||
private let travelTimeFormatter: DateComponentsFormatter = .init() | ||
|
||
@Published | ||
var routes: [Route] = [] | ||
|
||
init() { | ||
travelTimeFormatter.unitsStyle = .short | ||
} | ||
|
||
func loadRoutes() { | ||
let startPoint = Waypoint(coordinate: CLLocationCoordinate2D(latitude: 38.9131752, longitude: -77.0324047), | ||
name: "Mapbox") | ||
let stopPoint = Waypoint(coordinate: CLLocationCoordinate2D(latitude: 38.89065720, longitude: -77.0090701), | ||
name: "Capitol") | ||
let endPoint = Waypoint(coordinate: CLLocationCoordinate2D(latitude: 38.8977, longitude: -77.0365), | ||
name: "White House") | ||
let options = RouteOptions(waypoints: [startPoint, stopPoint, endPoint]) | ||
options.includesSteps = true | ||
options.routeShapeResolution = .full | ||
options.attributeOptions = [.congestionLevel, .maximumSpeedLimit] | ||
|
||
Directions.shared.calculate(options) { (session, result) in | ||
switch result { | ||
case let .failure(error): | ||
print("Error calculating directions: \(error)") | ||
case let .success(response): | ||
self.routes = response.routes ?? [] | ||
} | ||
} | ||
} | ||
|
||
func formattedDistance(for route: Route) -> String { | ||
return distanceFormatter.string(fromMeters: route.distance) | ||
} | ||
|
||
func formattedTravelTime(for route: Route) -> String { | ||
return travelTimeFormatter.string(from: route.expectedTravelTime)! | ||
} | ||
|
||
func formattedTypicalTravelTime(for route: Route) -> String { | ||
if let typicalTravelTime = route.typicalTravelTime, | ||
let formattedTypicalTravelTime = travelTimeFormatter.string(from: typicalTravelTime) { | ||
return formattedTypicalTravelTime | ||
} | ||
else { | ||
return "Not available" | ||
} | ||
} | ||
|
||
func stepDescriptions(for step: RouteStep) -> String { | ||
var description: String = "" | ||
let direction = step.maneuverDirection?.rawValue ?? "none" | ||
description.append("\(step.instructions) [\(step.maneuverType) \(direction)]") | ||
if step.distance > 0 { | ||
let formattedDistance = distanceFormatter.string(fromMeters: step.distance) | ||
description.append(" (\(step.transportType) for \(formattedDistance))") | ||
} | ||
return description | ||
} | ||
} | ||
|
||
struct ContentView: View { | ||
@ObservedObject | ||
var vm: DirectionsViewModel | ||
|
||
var body: some View { | ||
ScrollView { | ||
LazyVStack(spacing: 10, content: { | ||
ForEach(vm.routes, id: \.routeIdentifier) { route in | ||
VStack(alignment: .leading, spacing: 3) { | ||
headerView(for: route) | ||
ForEach(0..<route.legs.count, id: \.self) { legIdx in | ||
if let source = route.legs[legIdx].source?.name, | ||
let destination = route.legs[legIdx].destination?.name { | ||
Text("From '\(source)' to '\(destination)'").font(.title2) | ||
} | ||
else { | ||
Text("Steps:").font(.title2) | ||
} | ||
stepsView(for: route.legs[legIdx]) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
.padding(5) | ||
.onAppear { vm.loadRoutes() } | ||
} | ||
|
||
@ViewBuilder | ||
private func headerView(for route: Route) -> some View { | ||
VStack(alignment: .leading, spacing: 10) { | ||
HStack { | ||
Text("Route: ").fontWeight(.bold) | ||
Text(route.description) | ||
.fixedSize(horizontal: false, vertical: true) | ||
} | ||
HStack { | ||
Text("Distance: ").fontWeight(.bold) | ||
Text(vm.formattedDistance(for:route)) | ||
} | ||
HStack { | ||
Text("ETA: ").fontWeight(.bold) | ||
Text(vm.formattedTravelTime(for: route)) | ||
} | ||
HStack { | ||
Text("Typical travel time: ").fontWeight(.bold) | ||
Text(vm.formattedTypicalTravelTime(for: route)) | ||
} | ||
Divider() | ||
} | ||
} | ||
|
||
@ViewBuilder | ||
private func stepsView(for leg: RouteLeg) -> some View { | ||
LazyVStack(alignment: .leading, spacing: 5, content: { | ||
ForEach(0..<leg.steps.count, id: \.self) { stepIdx in | ||
HStack { | ||
Text("\(stepIdx + 1). ").fontWeight(.bold) | ||
Text(vm.stepDescriptions(for: leg.steps[stepIdx])) | ||
} | ||
.padding([.top, .bottom], 3) | ||
|
||
Divider() | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.