Last active
May 17, 2016 11:59
-
-
Save orazz/50e337754bcff979e8aa5ae4a8b09fd4 to your computer and use it in GitHub Desktop.
MapboxDirections
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
func calculateDirections(currentLocation:CLLocationCoordinate2D, distination: CLLocationCoordinate2D) { | |
self.calculatedTourPoints = [] | |
self.calculatedTour = [] | |
let request = MBDirectionsRequest(sourceCoordinate: currentLocation, destinationCoordinate: distination) | |
request.version = .Four | |
request.requestsAlternateRoutes = true | |
request.transportType = .Automobile | |
directions = MBDirections(request: request, accessToken: "pk.***") | |
directions!.calculateDirectionsWithCompletionHandler { (response, error) in | |
if let route = response?.routes.first { | |
print("Route summary:") | |
let steps = route.legs.first!.steps | |
print("Distance: \(route.distance) meters (\(steps.count) route steps) in \(route.expectedTravelTime / 60) minutes") | |
for step in steps { | |
print("\(step.instructions) \(step.distance) meters") | |
self.calculatedTour.append(step) | |
} | |
for geo in route.geometry { | |
self.calculatedTourPoints.append(geo) | |
} | |
self.tourLine = MGLPolyline(coordinates: &self.calculatedTourPoints, count: UInt(self.calculatedTourPoints.count)) | |
self.mapView.addAnnotation(self.tourLine) | |
} else { | |
print("Error calculating directions: \(error)") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment