【问题标题】:iPhone compass showing to a specific locationiPhone 指南针显示到特定位置
【发布时间】:2011-09-04 08:56:43
【问题描述】:

我是 Objective-c 的新手,所以如果我的问题太垃圾,我想道歉。

我正在编写指南针,但它正在工作(感谢教程)。 给出了实际位置(经度和纬度)。 我的指南针内的箭头是否有可能向我显示到特定位置(经度 2 和纬度 2)的方向? 我读过,我必须考虑 iphone 的磁性和真实方向。

【问题讨论】:

  • 你能分享那个教程链接吗?

标签: iphone objective-c xcode compass-geolocation direction


【解决方案1】:
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
    double heading = newHeading.trueHeading; //in degree relative to true north
    double bearing = ... //get current bearing in degree relative to true north
                         //with the JS library mentioned below it would be something like
                         //bearing = userLoc.bearingTo(destionation);

    //just two lines, the the sake of the example
    double rotationInDegree = (bearing - heading);  //the important line
    rotationInDegree = fmod((rotationInDegree + 360), 360);  //just to be on the safe side :-)
    compassViewPerhapsAsImage.transform=CGAffineTransformMakeRotation(DegreesToRadians(rotationInDegree));
}

计算方位的好链接:

【讨论】:

    【解决方案2】:

    如果您不需要它太准确,那么它相当容易。您需要做的就是计算两点之间的角度,并在指南针上显示角度。 (由于您还没有发布代码,我不确定您是否有类似setAngleForCompass 的方法,但您应该这样做!)试试这个:

    float dx = Longitude2 - Longitude1;
    float dy = Latitude2 - Latitude1;
    float angle = atan2(dy, dx);
    // Set the angle of the compass here.
    

    或者,您可以使用CMMotionManager 访问陀螺仪,这也会为您指明正确的方向。 (双关语!)希望有所帮助!

    【讨论】:

      【解决方案3】:

      我还需要计算前往某个位置的航向。我发现最好的方法是使用球面余弦定律。我创建了 C 函数来实现这一点,它们可用Here on github。听起来您需要 headingInDegrees 函数。这会给你一个真正的标题。

      【讨论】:

        【解决方案4】:

        来自 tadeldv 的 CLLocation-Bearing 项目: https://github.com/tadelv/CLLocation-Bearing

        CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;}
        CGFloat RadiansToDegrees(CGFloat radians) {return radians * 180 / M_PI;}
        
        - (void)calculating bearing {  
                float lat1 = DegreesToRadians(YOURCURRENTPOSITION.coordinate.latitude);
                float lng1 = DegreesToRadians(YOURCURRENTPOSITION.coordinate.longitude);
                float lat2 = DegreesToRadians(YOURTARGETPOSITION.coordinate.latitude);
                float lng2 = DegreesToRadians(YOURTARGETPOSITION.coordinate.longitude);
                float deltalng = lng2 - lng1;
                double y = sin(deltalng) * cos(lat2);
                double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(deltalng);
                double bearing = atan2(y, x) + 2 * M_PI;
                float bearingDegrees = RadiansToDegrees(bearing);
                bearingDegrees = (int)bearingDegrees % 360;
        
                NSLog(@"%d", (int)bearingDegrees);
        }
        

        这为您提供了北极和目标之间的天使。那么你必须添加/减去当前航向的角度。

        【讨论】:

          【解决方案5】:

          感谢我迟到的回答。 我正在研究一个指向特定方向的箭头。我现在所做的是,让这个箭头做一个动画,但我的问题是,它应该(起初)指向同一个方向,就像 iphone 是“朝向”一样。

          到目前为止我的代码:

          - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
          {double heading = newHeading.trueHeading;
          [self rotateCompass:heading];
          }
          
          - (void) rotateCompass: (double) degrees{
          [UIView beginAnimations:nil context:NULL];
          [UIView setAnimationDelegate:self];
          [UIView setAnimationDuration:2];
          [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
          [UIView commitAnimations];
          compassRose.transform = CGAffineTransformMakeRotation(degrees);
          }
          

          我有其他例程来获取纬度、经度和标签,以显示标题并且它有效。 我的箭无缘无故地疯狂旋转。 我希望箭头指向与 trueHeading 相同的方向。 这意味着 iPhone 的方式

          【讨论】:

          • 我觉得应该是double heading = newHeading.magneticHeading
          • xib 中的箭头图像应该向上指向手机的顶部
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-05
          • 1970-01-01
          • 2014-08-04
          • 1970-01-01
          相关资源
          最近更新 更多