使用起始位置和距离计算新坐标

时间:2016-02-18 15:41:24

标签: ios swift

我正在尝试获取一个点的坐标,即距离起始位置的设定距离,但最终结果是错误的。

首先,我计算起始位置和所需目的地之间的角度:

private func calculateAngleBetweenLocations(currentLocation: CLLocationCoordinate2D, targetLocation: CLLocationCoordinate2D) -> Double {
    let fLat = self.degreesToRadians(currentLocation.latitude);
    let fLng = self.degreesToRadians(currentLocation.longitude);
    let tLat = self.degreesToRadians(targetLocation.latitude);
    let tLng = self.degreesToRadians(targetLocation.longitude);
    let deltaLng = tLng - fLng

    let y = sin(deltaLng) * cos(tLat)
    let x = cos(fLat) * sin(tLat) - sin(fLat) * cos(tLat) * cos(deltaLng)

    let bearing = atan2(y, x)

    return self.radiansToDegrees(bearing)
}

然后,我给出一个距离来计算新点:

private func coordinatesForMovement(endLocation: CLLocationCoordinate2D, distance: Double) -> CLLocationCoordinate2D {
    let angle = self.calculateAngleBetweenLocations(self.currentLocation, targetLocation: endLocation)
    let x = self.currentLocation.latitude + distance * cos(angle)
    let y = self.currentLocation.longitude + distance * sin(angle)

    return CLLocationCoordinate2D(latitude: x, longitude: y)
}

结果是this(脚是起始位置,蓝色标记是目的地,红色标记是新计算点的位置)。我已经尝试以米和公里以及其他每个浮点位置传递距离,但从未得到正确的结果。有任何想法吗?

1 个答案:

答案 0 :(得分:1)

好的,经过多次挖掘后,我找到了this回答,这解决了我的问题。这是我在swift中的完整解决方案:

internal func moveToLocation(location: CLLocationCoordinate2D, distance: Double) {
    let angle = self.calculateAngleBetweenLocations(self.currentLocation, targetLocation: location)
    let newLocation = self.coordinates(self.currentLocation, atDistance: distance, atAngle: angle)

    self.moveUser(newLocation: newLocation) 
}

private func coordinates(startingCoordinates: CLLocationCoordinate2D, atDistance: Double, atAngle: Double) -> CLLocationCoordinate2D {
    let distanceRadians = atDistance / 6371
    let bearingRadians = self.degreesToRadians(atAngle)
    let fromLatRadians = self.degreesToRadians(startingCoordinates.latitude)
    let fromLonRadians = self.degreesToRadians(startingCoordinates.longitude)

    let toLatRadians = asin(sin(fromLatRadians) * cos(distanceRadians) + cos(fromLatRadians) * sin(distanceRadians) * cos(bearingRadians))
    var toLonRadians = fromLonRadians + atan2(sin(bearingRadians) * sin(distanceRadians) * cos(fromLatRadians), cos(distanceRadians) - sin(fromLatRadians) * sin(toLatRadians));

    toLonRadians = fmod((toLonRadians + 3 * M_PI), (2 * M_PI)) - M_PI

    let lat = self.radiansToDegrees(toLatRadians)
    let lon = self.radiansToDegrees(toLonRadians)

    return CLLocationCoordinate2D(latitude: lat, longitude: lon)
}

private func calculateAngleBetweenLocations(currentLocation: CLLocationCoordinate2D, targetLocation: CLLocationCoordinate2D) -> Double {
    let fLat = self.degreesToRadians(currentLocation.latitude);
    let fLng = self.degreesToRadians(currentLocation.longitude);
    let tLat = self.degreesToRadians(targetLocation.latitude);
    let tLng = self.degreesToRadians(targetLocation.longitude);
    let deltaLng = tLng - fLng

    let y = sin(deltaLng) * cos(tLat)
    let x = cos(fLat) * sin(tLat) - sin(fLat) * cos(tLat) * cos(deltaLng)

    let bearing = atan2(y, x)

    return self.radiansToDegrees(bearing)
}

private func degreesToRadians(x: Double) -> Double {
    return M_PI * x / 180.0
}

private func radiansToDegrees(x: Double) -> Double {
    return x * 180.0 / M_PI
}