Skip to content

Commit

Permalink
Merge pull request #418 from mapbox/jerrad/directions-use-result
Browse files Browse the repository at this point in the history
Use Result for directions response
  • Loading branch information
JThramer authored Apr 4, 2020
2 parents c915a0f + 0a0d243 commit c91d3ba
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 178 deletions.
100 changes: 53 additions & 47 deletions Directions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,45 +70,49 @@ class ViewController: UIViewController, MBDrawingViewDelegate {
options.routeShapeResolution = .full
options.attributeOptions = [.congestionLevel, .maximumSpeedLimit]

Directions.shared.calculate(options) { (response, error) in
if let error = error {
Directions.shared.calculate(options) { (session, result) in

switch (result) {
case let .failure(error):
print("Error calculating directions: \(error)")
return
}

if let route = response.routes?.first, let leg = route.legs.first {
print("Route via \(leg):")

let distanceFormatter = LengthFormatter()
let formattedDistance = distanceFormatter.string(fromMeters: route.distance)

let travelTimeFormatter = DateComponentsFormatter()
travelTimeFormatter.unitsStyle = .short
let formattedTravelTime = travelTimeFormatter.string(from: route.expectedTravelTime)

print("Distance: \(formattedDistance); ETA: \(formattedTravelTime!)")

for step in leg.steps {
let direction = step.maneuverDirection?.rawValue ?? "none"
print("\(step.instructions) [\(step.maneuverType) \(direction)]")
if step.distance > 0 {
let formattedDistance = distanceFormatter.string(fromMeters: step.distance)
print("\(step.transportType) for \(formattedDistance)")
case let .success(response):
if let route = response.routes?.first, let leg = route.legs.first {
print("Route via \(leg):")
let distanceFormatter = LengthFormatter()
let formattedDistance = distanceFormatter.string(fromMeters: route.distance)
let travelTimeFormatter = DateComponentsFormatter()
travelTimeFormatter.unitsStyle = .short
let formattedTravelTime = travelTimeFormatter.string(from: route.expectedTravelTime)
print("Distance: \(formattedDistance); ETA: \(formattedTravelTime!)")
for step in leg.steps {
let direction = step.maneuverDirection?.rawValue ?? "none"
print("\(step.instructions) [\(step.maneuverType) \(direction)]")
if step.distance > 0 {
let formattedDistance = distanceFormatter.string(fromMeters: step.distance)
print("\(step.transportType) for \(formattedDistance)")
}
}
}

if var routeCoordinates = route.shape?.coordinates, routeCoordinates.count > 0 {
// Convert the route’s coordinates into a polyline.
let routeLine = MGLPolyline(coordinates: &routeCoordinates, count: UInt(routeCoordinates.count))

// Add the polyline to the map.
self.mapView.addAnnotation(routeLine)

// Fit the viewport to the polyline.
let camera = self.mapView.cameraThatFitsShape(routeLine, direction: 0, edgePadding: .zero)
self.mapView.setCamera(camera, animated: true)
if var routeCoordinates = route.shape?.coordinates, routeCoordinates.count > 0 {
// Convert the route’s coordinates into a polyline.
let routeLine = MGLPolyline(coordinates: &routeCoordinates, count: UInt(routeCoordinates.count))

// Add the polyline to the map.
self.mapView.addAnnotation(routeLine)

// Fit the viewport to the polyline.
let camera = self.mapView.cameraThatFitsShape(routeLine, direction: 0, edgePadding: .zero)
self.mapView.setCamera(camera, animated: true)
}
}
}


}
}

Expand Down Expand Up @@ -144,8 +148,10 @@ class ViewController: UIViewController, MBDrawingViewDelegate {
func makeMatchRequest(locations: [CLLocationCoordinate2D]) {
let matchOptions = MatchOptions(coordinates: locations)

Directions.shared.calculate(matchOptions) { (response, error) in
if let error = error {
Directions.shared.calculate(matchOptions) { (session, result) in

switch result {
case let .failure(error):
let errorString = """
⚠️ Error Enountered. ⚠️
Failure Reason: \(error.failureReason ?? "")
Expand All @@ -155,19 +161,19 @@ class ViewController: UIViewController, MBDrawingViewDelegate {
"""
print(errorString)
return
case let .success(response):
guard let matches = response.matches, let match = matches.first else { return }
if let annotations = self.mapView.annotations {
self.mapView.removeAnnotations(annotations)
}

var routeCoordinates = match.shape!.coordinates
let coordCount = UInt(routeCoordinates.count)
let routeLine = MGLPolyline(coordinates: &routeCoordinates, count: coordCount)
self.mapView.addAnnotation(routeLine)
self.drawingView?.reset()
}

guard let matches = response.matches, let match = matches.first else { return }

if let annotations = self.mapView.annotations {
self.mapView.removeAnnotations(annotations)
}

var routeCoordinates = match.shape!.coordinates
let coordCount = UInt(routeCoordinates.count)
let routeLine = MGLPolyline(coordinates: &routeCoordinates, count: coordCount)
self.mapView.addAnnotation(routeLine)
self.drawingView?.reset()

}
}
}
Loading

0 comments on commit c91d3ba

Please sign in to comment.