0

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

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

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

输出是:loc: -1.0...

但为什么?

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

我做错了什么?

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

4

2 回答 2

3

-course 返回移动物体的方向。所以你会在你的用户当前位置调用 -course 。它不会为您提供从您所在位置到另一个位置的方向。因此,除非纽约市正在移动,否则它将始终返回 -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);
于 2013-11-07T23:34:15.420 回答
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];
于 2016-01-22T06:01:50.003 回答