Hi, I am trying to place a node at each step of an MKRoute given the gps coordinates. I have this code in my MapViewController to make the route and append each step to steps which is an array of each CLLocation of the steps.
func mapDirections(POIJSONName: String) {
let src = MKPointAnnotation()
let dst = MKPointAnnotation()
let pois = decodeJson(POIJSONName: POIJSONName)
for poi in pois {
let location = poi
if location.name == gateNo {
/
/
let srcCoordinate = CLLocationCoordinate2D(latitude: (self.locationManager.location?.coordinate.latitude)!, longitude: (self.locationManager.location?.coordinate.longitude)!)
let dstCoordinate = CLLocationCoordinate2D(latitude: location.lat, longitude: location.lng)
createAnnotation(pinLocation: dstCoordinate, title: ("Destination: " + gateNo))
src.coordinate = srcCoordinate
dst.coordinate = dstCoordinate
mapView.setRegion(MKCoordinateRegionMake(src.coordinate, MKCoordinateSpanMake(0.7,0.7)), animated: true)
let directionsRequest = MKDirectionsRequest()
let markSrc = MKPlacemark(coordinate: CLLocationCoordinate2DMake(src.coordinate.latitude, src.coordinate.longitude), addressDictionary: nil)
let markDst = MKPlacemark(coordinate: CLLocationCoordinate2DMake(dst.coordinate.latitude, dst.coordinate.longitude), addressDictionary: nil)
directionsRequest.source = MKMapItem(placemark: markSrc)
directionsRequest.destination = MKMapItem(placemark: markDst)
directionsRequest.transportType = MKDirectionsTransportType.walking
let directions = MKDirections(request: directionsRequest)
directions.calculate(completionHandler: {
response, error in
if error == nil {
self.routeToDest = response!.routes[0] as MKRoute
self.mapView.add(self.routeToDest.polyline)
for route in (response?.routes)! {
/
for step in route.steps {
let stepLocation = CLLocation(latitude: step.polyline.coordinate.latitude, longitude: step.polyline.coordinate.longitude)
self.createAnnotation(pinLocation: stepLocation.coordinate, title: "Route Step") /
self.steps.append(stepLocation)
}
}
}
})
}
}
}
I want to put the sphere at each Route Step. I then have this code in my AR viewcontroller:
func radiansToDegrees(_ radians: Double) -> Double {
return (radians) * (180.0 / Double.pi)
}
func degreesToRadians(_ degrees: Double) -> Double {
return (degrees) * (Double.pi / 180.0)
}
func getHeadingForDirectionFromCoordinate(from: CLLocation, to: CLLocation) -> Double {
let fLat = degreesToRadians(from.coordinate.latitude)
let fLng = degreesToRadians(from.coordinate.longitude)
let tLat = degreesToRadians(to.coordinate.latitude)
let tLng = degreesToRadians(to.coordinate.longitude)
let degree = radiansToDegrees(atan2(sin(tLng-fLng)*cos(tLat), cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(tLng-fLng)))
if degree >= 0 {
return degree
} else {
return degree + 360
}
}
func repositionTarget(stepLocation: CLLocation) {
let heading = getHeadingForDirectionFromCoordinate(from: userLoc!, to: stepLocation)
let delta = heading - self.heading
let sphere = SCNSphere(radius: 3.5)
let material = SCNMaterial()
material.diffuse.contents = UIImage(named: "plasmaSphere.png")
sphere.materials = [material]
let sphereNode = SCNNode(geometry: sphere)
let stepLocAR = ARItem(location: stepLocation, itemNode: sphereNode)
let distance = userLoc?.distance(from: stepLocAR.location)
if let node = stepLocAR.itemNode {
node.position = SCNVector3(x: Float(delta), y: 0, z: Float(-distance!))
sceneView.scene.rootNode.addChildNode(node)
}
}
func placeSteps() {
for step in self.steps {
let stepLocation = CLLocation(latitude: step.coordinate.latitude, longitude: step.coordinate.longitude)
repositionTarget(stepLocation: stepLocation)
}
}
and in my viewDidLoad to call placeSteps if the view controller loads from the correct segue:
/
if fromMap == true && steps != nil {
placeSteps()
}
but when I run the app, only 3 nodes are added and they are at seemingly random incorrect locations. Does anyone see where I'm going wrong? Thank