iPhone Compass GPS方向 [英] iPhone Compass GPS Direction
问题描述
我试图开发一个应用程序,使用iPhone的GPS和指南针来指向某个特定位置的某种指针(例如指南针始终指向北方)。该位置是固定的,我总是需要指针指向该特定位置,而不管用户位于何处。我有这个位置的纬度/经度坐标,但不知道如何使用指南针和GPS指向那个位置......就像 http://www.youtube.com/watch?v=iC0Xn8hY80w 此链接1:20'
I写一些代码,但是,它不能正确旋转。
- (float)angleToRadians:(double)a {
回报((a / 180)* M_PI);
$ b - (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];
/ *我使用这个,但它不能旋转:点即使我改变坐标
CGPointMake * /
Compass2.layer.anchorPoint = CGPointMake(0, 0.5);
[Compass2 setTransform:CGAffineTransformMakeRotation(temp1)];
/ * Compass2是一个UIImageView,如下图所示我想将它旋转
:point in image
^
|
|
|
:
|
* /
您可以使用标题或方位方程 - 如果您位于lat1,lon1,并且您感兴趣的点位于lat2,lon2,则方程为:
(lat2),cos(lat1)* sin(lat2)-sin(lat1)* cos(lat2)* cos(lat2)* sin(lat1) lon2-lon1))
这给你一个弧度方位,你可以通过乘以180 /π。这个值是在-180和180度之间,所以要得到一个标准的指南针方位,可以给360加上任何的负面答案。
atan2是一个与arctan相关的标准函数,为您的目的地可能会出现的四个象限做出正确的选择。
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
^
|
|
|
:
|
*/
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:
heading = atan2( sin(lon2-lon1)*cos(lat2), cos(lat1)*sin(lat2) - sin(lat1)*cos(lat2)*cos(lon2-lon1))
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.
这篇关于iPhone Compass GPS方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!