作为概念证明,我想要创建一个应用程序来检索当前坐标,计算朝向另一个点的方向,并使用指南针旋转箭头图像指向太空中的这一点。使用指南针标题旋转图像以指向熟知的点
我知道如何检索当前坐标并通过CGAffineTransformMakeRotation旋转图像,但我还没有找到计算正确角度的公式。
任何提示?
作为概念证明,我想要创建一个应用程序来检索当前坐标,计算朝向另一个点的方向,并使用指南针旋转箭头图像指向太空中的这一点。使用指南针标题旋转图像以指向熟知的点
我知道如何检索当前坐标并通过CGAffineTransformMakeRotation旋转图像,但我还没有找到计算正确角度的公式。
任何提示?
首先你需要计算一个方位。本页面给出了这样做的一个整洁的公式:
http://www.movable-type.co.uk/scripts/latlong.html
然后,你可以做一些简单的算术发现轴承和航向的iPhone指向朝之间的差异。按照这种差异旋转图像。
轴承是:
double bearingUsingStartCoordinate(CLLocation *start, CLLocation *end)
{
double tc1;
tc1 = 0.0;
//dlat = lat2 - lat1
//CLLocationDegrees dlat = end.coordinate.latitude - start.coordinate.latitude;
//dlon = lon2 - lon1
CLLocationDegrees dlon = end.coordinate.longitude - start.coordinate.longitude;
//y = sin(lon2-lon1)*cos(lat2)
double y = sin(d2r(dlon)) * cos(d2r(end.coordinate.latitude));
//x = cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(lon2-lon1)
double x = cos(d2r(start.coordinate.latitude))*sin(d2r(end.coordinate.latitude)) - sin(d2r(start.coordinate.latitude))*cos(d2r(end.coordinate.latitude))*cos(d2r(dlon));
if (y > 0)
{
if (x > 0)
tc1 = r2d(atan(y/x));
if (x < 0)
tc1 = 180 - r2d(atan(-y/x));
if (x == 0)
tc1 = 90;
} else if (y < 0)
{
if (x > 0)
tc1 = r2d(-atan(-y/x));
if (x < 0)
tc1 = r2d(atan(y/x)) - 180;
if (x == 0)
tc1 = 270;
} else if (y == 0)
{
if (x > 0)
tc1 = 0;
if (x < 0)
tc1 = 180;
if (x == 0)
tc1 = nan(0);
}
if (tc1 < 0)
tc1 +=360.0;
return tc1;
}
而对于那些两点之间寻找距离:
double haversine_km(double lat1, double long1, double lat2, double long2)
{
double dlong = d2r(long2 - long1);
double dlat = d2r(lat2 - lat1);
double a = pow(sin(dlat/2.0), 2) + cos(d2r(lat1)) * cos(d2r(lat2)) * pow(sin(dlong/2.0), 2);
double c = 2 * atan2(sqrt(a), sqrt(1-a));
double d = 6367 * c;
return d;
}
double haversine_mi(double lat1, double long1, double lat2, double long2)
{
double dlong = d2r(long2 - long1);
double dlat = d2r(lat2 - lat1);
double a = pow(sin(dlat/2.0), 2) + cos(d2r(lat1)) * cos(d2r(lat2)) * pow(sin(dlong/2.0), 2);
double c = 2 * atan2(sqrt(a), sqrt(1-a));
double d = 3956 * c;
return d;
}
OK,感谢该真棒页,我计算正确的轴承;不幸的是,我尝试了不同的公式来找出差异,但是我找不到合适的公式:请您指出正确的操作? – 2010-10-12 23:49:26
好吧,找到它,现在它的作品!非常感谢 – 2010-10-13 12:46:01