CLLocationManager和准确性问题-有经验吗? [英] CLLocationManager and accuracy issues - any experiences?

查看:121
本文介绍了CLLocationManager和准确性问题-有经验吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在处理iPhone GPS的一些准确性问题。我有一个使用位置的应用程序。

So I am dealing with some accuracy issues with iPhone GPS. I have an app which uses location.

在委托方法 locationManager:didUpdateToLocation:fromLocation:一个位置。从一点研究来看,即使将 desiredAccuracy 属性设置为 kCLLocationAccuracyBest 。

In the delegate method locationManager: didUpdateToLocation: fromLocation: I am being returned a location. From a bit of research, it appears the GPS isn't always accurate on the first result it returns, even when setting desiredAccuracy property to kCLLocationAccuracyBest.

为了解决这个问题,在返回之前,我不叫 stopUpdatingLocation newLocation:至少3次(这非常快)。对于是否要 stopUpdatingLocation 并返回 newLocation ,我也处理了另外两个要求。我尝试的一种方法是检查经纬度和经纬度以获取 newLocation 并与 oldLocation 进行比较,如果这些不一致,然后保持位置更新运行。我还尝试检查 oldLocation newLocation 之间的距离,如果距离小于20米,那就很好。都至少返回了3次测试,对这两种方法都进行了测试。后一种方式不太严格,因为 newLocation oldLocation 很难做到100%相同

In order to get around this, I don't call stopUpdatingLocation before it's returned newLocation: at least 3 times (this is very quick). I have also played around with two other "requirements" for whether to stopUpdatingLocation and return the newLocation. One way I tried was to check the lat and long for newLocation and compare to oldLocation and if these were not identical, then keep the location updating running. I also tried with checking the distance between the oldLocation and newLocation and if it's less than 20 meters, it's fine. Both of these are tested with the returning of at least 3 runs. The latter way is less "strict", since newLocation and oldLocation is quite hard to get away with being 100% identical if the user is in a moving vehicle.

现在,我的问题是,即使执行上述操作(基本上不接受位置,直到进行了一些更新) CLLocationManager 并检查 CLLocations 之间的距离(或它们是否相同),我仍然看到位置有些奇怪的结果有时候,在测试时。

Now, my issue is, that even when doing the above (basically not accepting a location, until a few updates have occured on CLLocationManager AND checking the distance between the CLLocations (or whether they are identical) I am still seeing somewhat weird results for locations sometimes, when testing.

有时候,如果我离开应用程序,进入Maps.app,使用GPS,然后打开多任务处理,强制退出我的应用程序,

It would be fixes sometimes if I leave the app, go into Maps.app, use the GPS, then open multitasking, force quit my app and then reopen it to get a clean launch.

人们过去用来解决同类问题的任何经验和可能的解决方案?感谢评论和解决方案:)

Any experiences and possible solutions people have used to get around the same kind of issue? Comments and solutions appreciated :)

推荐答案

我不记得是哪个项目正是我从下面的代码中获得的,但是这很糟糕非常适合我(我确实记得那是来自WWDC 2010的视频)。在我的代码中,我保留了原始项目中的注释,因此希望这会有所帮助。

I don't remember which project it was exactly that I got the following code from, but this has worked for me really well (I do remember it was from a WWDC 2010 video). In my code, I left the comments from the original project in tact so hopefully this will help.

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    // test the age of the location measurement to determine if the measurement is cached
    // in most cases you will not want to rely on cached measurements
    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];

    if (locationAge > 5.0) return;

    // test that the horizontal accuracy does not indicate an invalid measurement
    if (newLocation.horizontalAccuracy < 0) return;

    // test the measurement to see if it is more accurate than the previous measurement
    if (bestEffortAtLocation == nil || bestEffortAtLocation.horizontalAccuracy > newLocation.horizontalAccuracy) {
        // store the location as the "best effort"
        self.bestEffortAtLocation = newLocation;

        // test the measurement to see if it meets the desired accuracy
        //
        // IMPORTANT!!! kCLLocationAccuracyBest should not be used for comparison with location coordinate or altitidue 
        // accuracy because it is a negative value. Instead, compare against some predetermined "real" measure of 
        // acceptable accuracy, or depend on the timeout to stop updating. This sample depends on the timeout.
        //
        if (newLocation.horizontalAccuracy <= locationManager.desiredAccuracy) {
            // we have a measurement that meets our requirements, so we can stop updating the location
            // 
            // IMPORTANT!!! Minimize power usage by stopping the location manager as soon as possible.
            //
            [self stopUpdatingLocation:NSLocalizedString(@"Acquired Location", @"Acquired Location")];

            // we can also cancel our previous performSelector:withObject:afterDelay: - it's no longer necessary
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(stopUpdatingLocation:) object:nil];
        }
    }
}

希望这会有所帮助!

通过 GetLocationViewController.m 在Apple的 Locate Me示例项目中提供,位于:

Via GetLocationViewController.m in Apple's "Locate Me" sample project, available at:

https://developer.apple .com / library / content / samplecode / LocateMe / Introduction / Intro.html

这篇关于CLLocationManager和准确性问题-有经验吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆