我正在尝试开发一个使用 iPhone 的 GPS 和指南针的应用程序,以便将某种指针指向特定位置(就像指南针始终指向北方)。位置是固定的,无论用户位于何处,我始终需要指针指向该特定位置。我有这个位置的纬度/经度坐标,但不确定如何使用指南针和 GPS 指向该位置...就像 http://www.youtube.com/watch?v=iC0Xn8hY80w此链接 1:20'
我写了一些代码,但是它无法向右旋转。
-(float) angleToRadians:(double) a {
return ((a/180)*M_PI);
}
-(void)updateArrow {
double alon=[longi doubleValue];//source
double alat=[lati doubleValue];//source
double blon=[pointlongi doubleValue];//destination
double blat=[pointlati doubleValue];//destination
float fLat = [self angleToRadians:alat];
float fLng = [self angleToRadians:alon];
float tLat = [self angleToRadians:blat];
float tLng = [self angleToRadians:blon];
float temp = atan2(sin(tLng-fLng)*cos(tLat),
cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(tLng-fLng));
double temp2= previousHeading;
double temp1=temp-[self angleToRadians:temp2];
/*I using this,but it can't rotate by :point even i change the coordinate
in CGPointMake */
Compass2.layer.anchorPoint=CGPointMake(0, 0.5);
[Compass2 setTransform:CGAffineTransformMakeRotation(temp1)];
/* Compass2 is a UIImageView like below picture I want to rotate it around
: point in image
^
|
|
|
:
|
*/
最佳答案
您可以使用一个标准的“航向”或“方位”方程 - 如果您位于 lat1,lon1,并且您感兴趣的点位于 lat2,lon2,则方程为:
heading = atan2( sin(lon2-lon1)*cos(lat2), cos(lat1)*sin(lat2) - sin(lat1)*cos(lat2)*cos(lon2-lon1))
这将为您提供以弧度为单位的方位角,您可以通过乘以 180/π 将其转换为度数。该值介于 -180 和 180 度之间,因此要获得标准罗盘方位,请将 360 添加到任何否定答案。
atan2 是与 arctan 相关的标准函数,它对您的目的地点可能位于的四个可能的象限与您所在的位置进行比较,执行正确的操作。
关于iPhone 指南针 GPS 方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4130821/