【发布时间】:2013-07-30 07:06:51
【问题描述】:
我想计算 2 lat & long 之间的距离。我可以计算距离如下
CLLocation *currentLoc = [[CLLocation alloc] initWithLatitude:-24.4132995 longitude:121.0790024];
CLLocation *restaurnatLoc = [[CLLocation alloc] initWithLatitude:-32.8310013 longitude:150.1390075];
CLLocationDistance meters = [restaurnatLoc distanceFromLocation:currentLoc];
NSLog(@"Distance between 2 geo cordinates: %.2f Meters",meters);
现在我想获取从 currentLocation 到restaurnatLoc 的方向。为此,我有以下代码
double DegreesToRadians(double degrees) {return degrees * M_PI / 180;};
double RadiansToDegrees(double radians) {return radians * 180/M_PI;};
-(double) bearingToLocationFromCoordinate:(CLLocation*)fromLoc toCoordinate:(CLLocation*)toLoc
{
double lat1 = DegreesToRadians(fromLoc.coordinate.latitude);
double lon1 = DegreesToRadians(fromLoc.coordinate.longitude);
double lat2 = DegreesToRadians(toLoc.coordinate.latitude);
double lon2 = DegreesToRadians(toLoc.coordinate.longitude);
double dLon = lon2 - lon1;
double y = sin(dLon) * cos(lat2);
double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
double radiansBearing = atan2(y, x);
return RadiansToDegrees(radiansBearing);
}
它返回bearing = 114.975752 现在我如何确定餐厅是否在我当前位置的North、South、West、East、NW、NE、SW、SE?
我从这个链接Direction based off of 2 Lat,Long points得到1个解决方案但是如果我考虑这个解决方案,那么我怀疑从我的位置(红色圆圈)到餐厅(绿色圆圈)的轴承114,如下所示。如果我错了,请纠正我。
由于当前位置是“西澳大利亚”,餐厅位置是“悉尼”,如 Google 地图所示。
任何人都可以告诉我这里出了什么问题吗?谢谢。
////////////////////////更新 ///////// //////////////////
我的指南针图是错误的。感谢 AlexWien,这是正确的图表
现在我得到了正确的输出
【问题讨论】:
标签: ios gps cllocation direction bearing