Skip to content

Instantly share code, notes, and snippets.

@aclima93
Last active August 21, 2018 14:57
Show Gist options
  • Save aclima93/901e15838c3f1937a8df635ba1826f19 to your computer and use it in GitHub Desktop.
Save aclima93/901e15838c3f1937a8df635ba1826f19 to your computer and use it in GitHub Desktop.
ARKit & CoreLocation + POI + MapKit + Directions
func getRouteSegments(startCoordinate: CLLocationCoordinate2D, endCoordinate: CLLocationCoordinate2D) {
// Request the walking directions between the two points
let directionRequest = setupRouteDirectionsRequest(startCoordinate: startCoordinate, endCoordinate: endCoordinate)
let directions = MKDirections(request: directionRequest)
directions.calculate {
(response, error) -> Void in
guard let response = response else {
if let error = error {
print("Error: \(error)")
// TODO: handle error
}
return
}
// add route segments to AR Scene
self.manageRouteDirections(response)
}
}
func manageRouteDirections(_ response: MKDirections.Response) {
for route in response.routes {
// add route coordinates together so we can build our AR path
var routeCoordinates = [CLLocationCoordinate2D]()
for step in route.steps {
let pointCount = step.polyline.pointCount
let stepCoordinates = UnsafeMutablePointer<CLLocationCoordinate2D>.allocate(capacity: pointCount)
step.polyline.getCoordinates(stepCoordinates, range: NSRange(location: 0, length: pointCount))
for i in 0..<pointCount {
routeCoordinates.append(stepCoordinates[i])
}
}
// create and add the route segments to the AR scene
for i in 0..<routeCoordinates.count-1 {
let startCoordinate = routeCoordinates[i]
let endCoordinate = routeCoordinates[i+1]
let routeSegment = RouteSegment(startLatitude: startCoordinate.latitude, startLongitude: startCoordinate.longitude, startAltitude: 0,
endLatitude: endCoordinate.latitude, endLongitude: endCoordinate.longitude, endAltitude: 0)
self.addRouteSegmentToARScene(routeSegment)
}
// handle destination segment
let finalStepCoordinate = routeCoordinates[routeCoordinates.count-1]
let routeSegment = RouteSegment(startLatitude: finalStepCoordinate.latitude, startLongitude: finalStepCoordinate.longitude, startAltitude: 0,
endLatitude: selectedPOI!.latitude, endLongitude: selectedPOI!.longitude, endAltitude: 0)
self.addFinalRouteSegmentToARScene(routeSegment)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment