【问题标题】:Rotate an image to direct to a well know point using compass heading使用罗盘航向旋转图像以指向众所周知的点
【发布时间】:2010-10-11 22:07:15
【问题描述】:

作为概念证明,我想创建一个应用程序来检索当前坐标,计算指向另一个点的方向,并使用指南针旋转箭头图像以指向空间中的那个点。

我知道如何检索当前坐标并通过 CGAffineTransformMakeRotation 旋转图像,但我还没有找到计算正确角度的公式。

有什么提示吗?

【问题讨论】:

    标签: iphone cocoa-touch compass-geolocation image-rotation


    【解决方案1】:

    首先您需要计算方位角。此页面提供了一个简洁的公式:

    http://www.movable-type.co.uk/scripts/latlong.html

    然后,您可以进行一些简单的算术运算来找出该方位与 iPhone 指向的航向之间的差异。按该差异旋转您的图像。

    【讨论】:

    • 好的,感谢那个很棒的页面,我计算出正确的方位;不幸的是,我尝试了不同的公式来找出不同之处,但我没有找到正确的:请您指出正确的操作吗?
    【解决方案2】:

    轴承是:

    double bearingUsingStartCoordinate(CLLocation *start, CLLocation *end)
    {
        double tc1;
        tc1 = 0.0;
    
        //dlat = lat2 - lat1
        //CLLocationDegrees dlat = end.coordinate.latitude - start.coordinate.latitude; 
    
        //dlon = lon2 - lon1
        CLLocationDegrees dlon = end.coordinate.longitude - start.coordinate.longitude;
    
        //y = sin(lon2-lon1)*cos(lat2)
        double y = sin(d2r(dlon)) * cos(d2r(end.coordinate.latitude));
    
        //x = cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(lon2-lon1)
        double x = cos(d2r(start.coordinate.latitude))*sin(d2r(end.coordinate.latitude)) - sin(d2r(start.coordinate.latitude))*cos(d2r(end.coordinate.latitude))*cos(d2r(dlon)); 
    
        if (y > 0)
        {
            if (x > 0)
                tc1 = r2d(atan(y/x));
    
            if (x < 0)
                tc1 = 180 - r2d(atan(-y/x));
    
            if (x == 0)
                tc1 = 90;
    
        } else if (y < 0)
        {
            if (x > 0)
                tc1 = r2d(-atan(-y/x));
    
            if (x < 0)
                tc1 = r2d(atan(y/x)) - 180;
    
            if (x == 0)
                tc1 = 270;
    
        } else if (y == 0)
        {
            if (x > 0)
                tc1 = 0;
    
            if (x < 0)
                tc1 = 180;
    
            if (x == 0)
                tc1 = nan(0);
        }
        if (tc1 < 0)
            tc1 +=360.0;
            return tc1;
    }
    

    对于那些寻找两点之间距离的人:

    double haversine_km(double lat1, double long1, double lat2, double long2)
    {
        double dlong = d2r(long2 - long1);
        double dlat = d2r(lat2 - lat1);
        double a = pow(sin(dlat/2.0), 2) + cos(d2r(lat1)) * cos(d2r(lat2)) * pow(sin(dlong/2.0), 2);
        double c = 2 * atan2(sqrt(a), sqrt(1-a));
        double d = 6367 * c;
    
        return d;
    }
    
    double haversine_mi(double lat1, double long1, double lat2, double long2)
    {
        double dlong = d2r(long2 - long1);
        double dlat = d2r(lat2 - lat1);
        double a = pow(sin(dlat/2.0), 2) + cos(d2r(lat1)) * cos(d2r(lat2)) * pow(sin(dlong/2.0), 2);
        double c = 2 * atan2(sqrt(a), sqrt(1-a));
        double d = 3956 * c; 
    
        return d;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多