iPhone 指南针 GPS 方向

标签 iphone gps location compass-geolocation

我正在尝试开发一个使用 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/

相关文章:

android - 如何从 GPS 计算移动平均速度?

android - 创建 Android 位置对象

ios - CLLocationManager.location 为零

iphone - 在 IOS 中隐藏状态栏时显示网络事件指示器

iphone - 为 iOS 创建自定义 UIController 分步教程

iphone - 从 sqlite 检索 BLOB 图像

带有 kCLLocationAccuracyKilometer 和 GPS 图标的 ios 后台位置

Android-需要在不开启GPS的情况下查找GPS信号强度

javascript - 我需要回答的关于 setTimeout 的问题

iphone - MFMailComposeViewController 在 canSendMail 上超时?