我想知道遥远对象的方位角(课程)(在地图上)。我有这个遥远的对象的坐标。我试图这样做:

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

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

输出是: loc: -1.0...

但为什么?

我想从我目前的方向了解这门课程。我也有位置更新 startUpdatingLocation 方法和委托更新。

我做错了什么?

为什么我不能仅仅从当前职位上获得遥远的对象?谢谢。

有帮助吗?

解决方案

- 课程返回移动对象的方向。因此,您会在用户当前位置上致电 - 课程。它不会为您提供从位置到另一个位置的方向。因此,除非纽约市移动,否则它将始终返回-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);

其他提示

使用以下代码获取课程

CLLocation *distLoc =  [[CLLocation  alloc] initWithCoordinate:loc altitude:location.altitude horizontalAccuracy:location.horizontalAccuracy verticalAccuracy:location.verticalAccuracy  course:location.course speed:location.speed timestamp:location.timestamp];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top