Skip to content

Instantly share code, notes, and snippets.

@orazz
Last active May 17, 2016 11:59
Show Gist options
  • Save orazz/50e337754bcff979e8aa5ae4a8b09fd4 to your computer and use it in GitHub Desktop.
Save orazz/50e337754bcff979e8aa5ae4a8b09fd4 to your computer and use it in GitHub Desktop.
MapboxDirections
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