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

标签 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是结果(脚是起始位置,蓝色标记是目的地,红色标记是新计算点所在的位置)。我尝试过以米和公里为单位传递距离以及所有其他浮点位置,但从未得到正确的结果。有任何想法吗?

最佳答案

好吧,经过一番挖掘,我发现 this答案,这解决了我的问题。这是我的完整解决方案:

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
}

关于ios - 使用起始位置和距离计算新坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52683664/

相关文章:

swift - 一旦答案明确,Swift 中是否有一个运算符会停止对多表达式条件语句的求值?

swift 3 numberofrowsinsection 在 vi​​ewwillappear 获取提醒之前运行

ios - 自定义 SearchBar iOS

ios - 系统提供的调用屏幕上扬声器按钮的异常行为

ios - AFNetworking:在后台解析 xml

iOS6 : MFMailComposeViewController is not displaying

iOS后台上传图片

iOS 快速展开 2 个 View Controller 并显示另一个

ios - 替换什么[读者解雇ModalViewControllerAnimated :NO with?

ios - 自定义 UILabel 类中的圆角半径