Skip to content

Instantly share code, notes, and snippets.

@chriswebb09
Last active October 11, 2017 08:03
Show Gist options
  • Save chriswebb09/b5a1e5691a7851e20d0606d6df099485 to your computer and use it in GitHub Desktop.
Save chriswebb09/b5a1e5691a7851e20d0606d6df099485 to your computer and use it in GitHub Desktop.
import MapboxDirections
import Turf
import ARKit
public class NavigationService {
public init() { }
public static func navigate(from startingLocation: CLLocation, to destinationLocation: CLLocation, completion: @escaping ([Waypoint]?, [Route]?, Error?) -> Void) {
let waypoints = [
Waypoint(coordinate: CLLocationCoordinate2D(latitude: startingLocation.coordinate.latitude, longitude: startingLocation.coordinate.longitude), name: "start"),
Waypoint(coordinate: CLLocationCoordinate2D(latitude: destinationLocation.coordinate.latitude, longitude: destinationLocation.coordinate.longitude), name: "destination"),
]
let options = RouteOptions(waypoints: waypoints, profileIdentifier: .walking)
options.includesSteps = true
Directions.shared.calculate(options) { waypoints, routes, error in
if let error = error {
completion(nil, nil, error)
} else if let waypoints = waypoints, let routes = routes {
completion(waypoints, routes, nil)
}
}
}
public static func coordinatesForDistance(leg: RouteLeg) -> [CLLocation] {
var coordinates: [CLLocation] = []
var polyline = [CLLocationCoordinate2D]()
for step in leg.steps {
let coordinate = step.coordinates!.first!
polyline.append(coordinate)
}
let metersPerNode: CLLocationDistance = 5
let turfPolyline = Polyline(polyline)
for i in stride(from: metersPerNode, to: turfPolyline.distance() - metersPerNode, by: metersPerNode) {
if let nextCoordinate = turfPolyline.coordinateFromStart(distance: i) {
let interpolatedStepLocation = CLLocation(latitude: nextCoordinate.latitude, longitude: nextCoordinate.longitude)
coordinates.append(interpolatedStepLocation)
}
}
return coordinates
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment