-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);