swift - 计算两个位置之间的方位

标签 swift cllocation

我想计算 Swift 中两个位置之间的方位角。我尝试了一些代码,但它不起作用。我搜索了这个问题,但我没有找到任何关于这个的结果。

func calculat(userlocation:CLLocation){

    let latuserlocation: () = DegreesToRadians(userlocation.coordinate.latitude)

    let lonuserlocatioc: () = DegreesToRadians(userlocation.coordinate.longitude)


    latitude = NSString (string: places[activePlace]["lat"]!).doubleValue

    longitude = NSString (string: places[activePlace]["lon"]!).doubleValue


    let targetedPointLatitude: () = DegreesToRadians(latitude)

    let targetedPointlongitude: () = DegreesToRadians(longitude)


    let dlon = lonuserlocatioc  - targetedPointlongitude


    let y = sin(dLon) * cos(targetedPointLatitude);

    let x = cos(latuserlocation) * sin(targetedPointLatitude) - sin(latuserlocation) * cos(targetedPointLatitude) * cos(dLon);

    let radiansBearing = atan2(y, x);

return RadiansToDegrees(radiansBearing)

let dlon = lonuserlocatioc - targetedPointlongitude 的错误是:

(cannot invoke '-' with an argument list of type '((), ())')

最佳答案

CLLocation Category for Calculating Bearing w/ Haversine function 相比,您的 dlon 不同。这个答案有

let dlon = targetedPointlongitude - lonuserlocatioc

我不知道这是不是你的问题,但它看起来很奇怪。

关于swift - 计算两个位置之间的方位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29431835/

相关文章:

ios - 添加和删​​除事务队列观察器 - 正确的方法?

swift - CIFilter CISmoothLinearGradient 在 Swift 中传递颜色时崩溃

ios - 从 CLLocationManager 中过滤掉不良位置

ios - CLLocation 不会开始更新位置

ios - Xcode ScrollView 不滚动

arrays - 如何在 Swift 中合并不同对象的数组

iOS : alert user for specific Geo location area (lat, 长)

ios - CLLocationManager 一段时间后未在后台接收更新

json - 期望解码字符串,但找到了字典

ios - CLLocation 速度不显示在 UIView 上