iPhone 指南针 GPS 方向 [英] iPhone Compass GPS Direction

查看:33
本文介绍了iPhone 指南针 GPS 方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个使用 iPhone 的 GPS 和指南针的应用程序,以便将某种指针指向特定位置(例如指南针总是指向北方).该位置是固定的,无论用户位于何处,我总是需要指针指向该特定位置.我有这个位置的纬度/经度坐标,但不知道如何使用指南针和 GPS 指向那个位置......就像 http://www.youtube.com/watch?v=iC0Xn8hY80w 这个链接 1:20'

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

        ^
        |
        | 
        |
        :
        |
    */

推荐答案

有一个标准的航向"或方位"方程可以使用——如果你在 lat1,lon1,你感兴趣的点是在 lat2,lon2,则方程为:

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))

这为您提供了以弧度为单位的方位,您可以通过乘以 180/π 将其转换为度数.然后该值介于 -180 和 180 度之间,因此要获得标准指南针方位,请在任何否定答案上加上 360.

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 是一个与 arctan 相关的标准函数,与您所在的位置相比,它对您的目标点可能位于的四个可能的象限执行正确的操作.

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 指南针 GPS 方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆