Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix minor error with placement of route steps #255

Merged
merged 9 commits into from
Dec 1, 2019
13 changes: 13 additions & 0 deletions Sources/ARKit-CoreLocation/Extensions/CLLocation+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ public extension CLLocation {
let x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon)
return atan2(y, x).radiansToDegrees
}

/// Returns the midpoint between two locations
/// Note: Only usable for short distances like MKPolyline segments
func approxMidpoint(to: CLLocation) -> CLLocation {
return CLLocation(
coordinate: CLLocationCoordinate2D(
latitude: (coordinate.latitude + to.coordinate.latitude) / 2,
longitude: (coordinate.longitude + to.coordinate.longitude) / 2
),
altitude: (altitude + to.altitude) / 2
)
} // approxMidpoint(to:)

}

public extension CLLocation {
Expand Down
1 change: 1 addition & 0 deletions Sources/ARKit-CoreLocation/Nodes/LocationNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ open class LocationNode: SCNNode {
}
} else {
//Calculates distance based on the distance within the scene, as the location isn't yet confirmed
//TODO: This yields zero, perhaps we should investigate
adjustedDistance = Double(position.distance(to: position))

scale = SCNVector3(x: 1, y: 1, z: 1)
Expand Down
8 changes: 3 additions & 5 deletions Sources/ARKit-CoreLocation/Nodes/PolylineNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ private extension PolylineNode {
for i in 0 ..< polyline.pointCount - 1 {
let currentLocation = CLLocation(coordinate: points[i].coordinate, altitude: altitude)
let nextLocation = CLLocation(coordinate: points[i + 1].coordinate, altitude: altitude)
let midLocation = currentLocation.approxMidpoint(to: nextLocation)

let distance = currentLocation.distance(from: nextLocation)

Expand All @@ -82,13 +83,10 @@ private extension PolylineNode {

let bearing = -currentLocation.bearing(between: nextLocation)

// This pivot translation in the +Z direction will cause the box to be
// rendered a bit away from its true location. The actual factor should
// probably be a parameter or property of some sort.
boxNode.pivot = SCNMatrix4MakeTranslation(0, 0, 0.5 * Float(distance))
// Orient the line to point from currentLoction to nextLocation
boxNode.eulerAngles.y = Float(bearing).degreesToRadians

let locationNode = LocationNode(location: currentLocation, tag: tag)
let locationNode = LocationNode(location: midLocation, tag: tag)
locationNode.addChildNode(boxNode)

locationNodes.append(locationNode)
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Changelog
- 1.2.2
- [PR #255 - Fix minor error with placement of route steps](https://github.com/ProjectDent/ARKit-CoreLocation/pull/255)
- [PR #249 - Fix Memory Leak](https://github.com/ProjectDent/ARKit-CoreLocation/pull/249)
- [PR #244 - Improve geodesy calculations and test coverage](https://github.com/ProjectDent/ARKit-CoreLocation/pull/244)
- [PR #240 - Fix build error, update compiler checks](https://github.com/ProjectDent/ARKit-CoreLocation/pull/240)
Expand Down