我想计算2 lat&之间的距离。长。我能够计算下面的距离
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);
}
返回方位= 114.975752现在我如何从当前位置决定餐厅是北,南,西,东,NW,NE,SW,SE?
我从这个链接获得1个解决方案Direction based off of 2 Lat,Long points但是如果我考虑这个解决方案,那么我怀疑从我的位置(红色圆圈)到餐厅(绿色圆圈)承载114,如下所示。如果我错了,请纠正我。
目前的位置是“西澳大利亚”&餐厅位置为“悉尼”,如谷歌地图所示。
任何人都可以告诉我这里出了什么问题吗?感谢。
///////////////////////////// 更新 ////////// ///////////////////
我的罗盘图错了。这是正确的图表,这要归功于AlexWien
现在我得到了正确的输出
答案 0 :(得分:2)
地理方向是顺时针测量的!
所以东经114度,符合你的预期