简体   繁体   English

使用CLLocationManager获取准确的位置

[英]Getting an accurate location with CLLocationManager

I'm beginning to mess around with the CLLocationManager and I am trying to use the callback method below. 我开始乱用CLLocationManager,我正在尝试使用下面的回调方法。 I went outside to test it on my iPhone, and with the first few updates, I seem to be getting gibberish values, like all of a sudden my distance is 39meters even though I haven't moved anywhere. 我去外面在我的iPhone上测试它,并且在最初的几次更新中,我似乎得到了胡言乱语的价值,就像突然我的距离是39米,即使我没有移动到任何地方。 Or sometimes it will start at 5, and then jump to 20 meters, again without me moving anywhere. 或者有时它会从5开始,然后跳到20米,再没有我移动到任何地方。 I went outside and walked, and the updates from the initial starting point seemed 'ok,' and then when I walked back, I got back to the original 39 meters starting point. 我走到外面走了,从最初的起点开始的更新似乎“没问题”,然后当我走回去时,我回到原来的39米起点。 I was wondering if what I am doing below is correct. 我想知道我在下面做的是否正确。 I also included my viewDidLoad method where I initialize my CLLocationManager object. 我还包括了我的viewDidLoad方法,我初始化了我的CLLocationManager对象。

Is there a way to ensure that my first values are accurate? 有没有办法确保我的第一个值是准确的? Thanks in advance. 提前致谢。

- (void)viewDidLoad
{
    [super viewDidLoad];
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    if (startingPoint == nil) {
        NSLog(@"NIL starting point");
        self.startingPoint = newLocation;
    }

    CLLocationDistance distance = [newLocation distanceFromLocation:startingPoint];
    NSString *distanceString = [[NSString alloc] initWithFormat:@"%g m", distance];
    distanceLabel.text = distanceString;
    [distanceString release];

    numberOfUpdates++;
    NSString *countString = [[NSString alloc] initWithFormat:@"%d", numberOfUpdates];
    countLabel.text = countString;
    [countString release];
}

GPS is an imperfect technology. GPS是一种不完美的技术。 Atmospheric conditions, sattelite availability (and position), sky visibility, signals bouncing off nearby objects (buildings) all contribute to it's inherent inaccuracy. 大气条件,卫星可用性(和位置),天空能见度,从附近物体(建筑物)反弹的信号都会导致其固有的不准确性。 Though there is even an "accuracy" value (which is usually pretty good) - even this is not completely reliable. 虽然甚至有一个“准确度”值( 通常很好) - 即使这不是完全可靠的。

Airplanes are not allowed to use GPS for precision approaches - even their receivers aren't accurate enough, and they require other technologies (which have their own issues). 飞机不允许使用GPS进行精确进近 - 即使它们的接收器不够精确,也需要其他技术(有自己的问题)。

Try running the standard "Maps" application and use it as a comparison. 尝试运行标准的“地图”应用程序并将其用作比较。 I think your code is good - it's jut GPS. 我认为你的代码很好 - 这是突然的GPS。

Of course I am saying this because I am working on my own maritime navigation application, an running into all these issues myself. 我当然是这样说的,因为我正在研究自己的海事导航应用程序,我自己也遇到了所有这些问题。

Though this is an old question I still like to answer. 虽然这是一个老问题,我仍然想回答。 The first call to "didUpdateToLocation" usually is some old value and you should always check the timestamp. 第一次调用“didUpdateToLocation”通常是一些旧值,您应该始终检查时间戳。 From the Apple documentation: 从Apple文档:

NSDate* eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 15.0)
{
   // use the value
}

在您的didUpdate方法中,您可以测试newLocation.horizontalAccuracy属性以获得可接受的值,并抛出任何不可能的值(甚至不太可能!)。

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

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