iPhone 指南针 GPS 方向
我正在尝试开发一个使用 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
^
|
|
|
:
|
*/
I'm trying to develop an application that use the GPS and Compass of the iPhone in order to point some sort of pointer to a specific location (like the compass always point to the North). The location is fixed and I always need the pointer to point to that specific location no matter where the user is located. I have the Lat/Long coordinates of this location but not sure how can I point to that location using the Compass and the GPS... just like http://www.youtube.com/watch?v=iC0Xn8hY80w this link 1:20'
I write some code, however, it can't rotate right direction.
-(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
^
|
|
|
:
|
*/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用一个标准的“航向”或“方位角”方程 - 如果您位于 lat1,lon1,并且您感兴趣的点位于 lat2,lon2,则方程为:
这为您提供了以弧度为单位的方位角,您可以通过乘以 180/π 将其转换为度数。该值介于 -180 和 180 度之间,因此要获得标准罗盘方位,请将 360 添加到任何否定答案。
atan2 是与 arctan 相关的标准函数,它对您的目的地点可能位于的四个可能的象限与您所在的位置进行比较,执行正确的操作。
There is a standard "heading" or "bearing" equation that you can use - if you are at lat1,lon1, and the point you are interested in is at lat2,lon2, then the equation is:
This gives you a bearing in radians, which you can convert to degrees by multiplying by 180/π. The value is then between -180 and 180 degrees, so to get a standard compass bearing add 360 to any negative answers.
atan2 is a standard function related to arctan, that does the right thing for the four possible quadrants that your destination point could be in compared to where you are.
1) 获取您当前的位置(从 GPS)
2) 获取纬度和经度的差异
3) 使用 atan2 方法获取角度,
即(警告:未经测试的代码)
您可能需要对其进行调整才能编译,但想法就在那里!
1) Get your current location (from the GPS)
2) Get the differences in latitude and longitude
3) use the atan2 method to get the angle
i.e. (WARNING: untested code)
You might have to tweak that to get it to compile but the idea is there!
我前段时间这样做过,这里有两种不同的实现。第一个与您的方法类似,第二个没有三角数学。第一个是我在应用程序中使用的,但第二个似乎也可以工作,尽管看起来不那么干净。您还需要记住根据用户界面中的北向来偏移此方位角。
还有第二个。
}
I did this some time ago, here are two different implementations. The first is similar to your approach, the second is without the trig math. The first is what I used in my app, but the second seemed to work as well, though doesn't appear to be as clean. You will need to also remember to offset this bearing based on north in your UI.
And the second.
}
用这个。您必须从 getHeadingForDirection 的结果中减去实际的罗盘航向,以确定正确的相对航向。返回值以弧度为单位。
Use this. You will have to subtract out your actual compass heading from the result of getHeadingForDirection to determine the proper relative heading. Return value is heading in radians.