简体   繁体   English

在iPhone上实现水平罗盘-算法?

[英]Implementing a horizontal compass on the iPhone - algorithm?

A horizontal compass looks something like this if you are facing due East (90 degrees). 如果您正对着东方(90度),水平指南针看起来像这样。

85----90---95

If you were facing due 355 degrees northwest, it would look like this: 如果您正对西北355度,则可能是这样的:

350----355---0

As you turn the compass, the number should cycle from 0 -> 360 -> 0 转动指南针时,数字应从0-> 360-> 0循环

So, my question is, how would you implement a view like this on the iPhone? 因此,我的问题是,您将如何在iPhone上实现这样的视图? I had a couple of ideas: 我有两个想法:

  1. Make one long image with all numbers and tick marks, and shift it left/right when the compass heading changes 制作一张包含所有数字和刻度线的长图像,并在指南针方向改变时向左/向右移动

  2. Create pieces of the view as tiles and append them when the compass heading changes. 将视图片段创建为图块,并在指南针方向更改时将其附加。

  3. Create a line of tick marks that shifts with the compass heading, and just write numbers on it as needed. 创建一行刻度线,该刻度线随指南针的方向移动,并根据需要在其上写下数字。

How would you attack this problem? 您将如何解决这个问题? Im mainly looking for algorithmic advice, but if you ave code or pseudo-code to demonstrate, that would be helpful too. 我主要是在寻找算法建议,但是如果您使用代码或伪代码进行演示,那也会有所帮助。

Option one is the easiest. 选项一是最简单的。 Keep in mind that you can composite part of an image to deal with the wrap-around. 请记住,您可以合成图像的一部分以处理环绕效果。

Here is a complete solution of implementing horizontal compass in iPhone 4 这是在iPhone 4中实现水平罗盘的完整解决方案

if ([CLLocationManager locationServicesEnabled] && [CLLocationManager headingAvailable]) 
    {
        m_locationManager=[[CLLocationManager alloc]init];
        m_locationManager.desiredAccuracy=kCLLocationAccuracyBest;
        m_locationManager.headingOrientation=CLDeviceOrientationPortrait;;
        m_locationManager.delegate=self;
        [m_locationManager startUpdatingHeading];

    }

pragma mark CLLocationManagerDelegate Methods 实用标记CLLocationManagerDelegate方法

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
    CLLocationDirection direction= newHeading.magneticHeading;
    [m_readingsLabel setText:[NSString stringWithFormat:@"Degrees:%f",direction]];

    float radians=direction*M_PI/180;
    m_compassImageView.transform=CGAffineTransformMakeRotation(radians);
}

您可以构建标题的模10,并切换10、5和所有其他模。

You just need to implement location manager functionality to find the exact direction in which your iPhone is pointing. 您只需要实现位置管理器功能,即可找到iPhone指向的确切方向。

So, You can check this : 因此,您可以检查以下内容:

CLLocationManager *locationManager;
[locationManager startUpdatingHeading];

And whenever you call startUpdatingHeading() it will call " - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading " method which you need to override from Location manager. 每当调用startUpdatingHeading()时,它将调用“ - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading ”方法,您需要从位置管理器中重写该方法。

And that's it you can find the direction in which iPhone 3GS is pointing by newHeading.magneticHeading or newHeading.trueHeading. 就是这样,您可以通过newHeading.magneticHeading或newHeading.trueHeading找到iPhone 3GS指向的方向。

You will surprise that you can get exact the same direction what magnetic compass gives you. 您会惊讶地发现,您可以获得与电磁罗盘完全相同的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM