【问题标题】:Get course of distant object with CLLocation使用 CLLocation 获取远处物体的路线
【发布时间】:2013-11-19 20:15:56
【问题描述】:

我想知道远处物体(在地图上)的方位角(航向)。 我有这个遥远物体的坐标。我试着这样做:

CLLocation *distLoc = [CLLocation alloc] initWithLatitude:40.725405 
                                                longitude:-74.003906]; // NY city

NSLog(@"loc: %f", distLoc.course);

输出为:loc: -1.0...

但是为什么呢?

我想从我目前的方向了解这门课程。我还使用startUpdatingLocation 方法和委托更新进行位置更新。

我做错了什么?

为什么我不能从当前位置获取远处物体的路线?谢谢。

【问题讨论】:

  • 邮政编码您如何获得距离。 (除了上面的日志输出)。

标签: ios objective-c geolocation core-location cllocation


【解决方案1】:

使用以下代码获取课程

CLLocation *distLoc =  [[CLLocation  alloc] initWithCoordinate:loc altitude:location.altitude horizontalAccuracy:location.horizontalAccuracy verticalAccuracy:location.verticalAccuracy  course:location.course speed:location.speed timestamp:location.timestamp];

【讨论】:

    【解决方案2】:

    -course 返回移动对象的方向。所以你会在你的用户当前位置调用 -course 。它不会为您提供从您所在位置到另一个位置的方向。因此,除非 NYC 正在移动,否则它将始终返回 -1

    如果你想找到你应该移动的指南针方向,你可以这样做:

    #define RAD_TO_DEG(r) ((r) * (180 / M_PI))
    
    ...    
    
    CLLocationCoordinate2D coord1 = currentLocation.coordinate;
    CLLocationCoordinate2D coord2 = distLoc.coordinate;
    
    CLLocationDegrees deltaLong = coord2.longitude - coord1.longitude;
    CLLocationDegrees yComponent = sin(deltaLong) * cos(coord2.latitude);
    CLLocationDegrees xComponent = (cos(coord1.latitude) * sin(coord2.latitude)) - (sin(coord1.latitude) * cos(coord2.latitude) * cos(deltaLong));
    
    CLLocationDegrees radians = atan2(yComponent, xComponent);
    CLLocationDegrees degrees = RAD_TO_DEG(radians) + 360;
    
    CLLocationDirection heading = fmod(degrees, 360);
    

    【讨论】:

    • 我在第一次更新后几分钟后调用它。我不需要距离,我正在寻找课程。 distLoc 我的意思是这里很远。
    • 哦,我明白了,我必须自己做标题,没有这个功能。感谢您提供代码示例,我会将此答案标记为已接受。谢谢。
    猜你喜欢
    • 2011-02-16
    • 1970-01-01
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 2019-01-03
    • 2021-03-06
    相关资源
    最近更新 更多