OGeek|极客世界-中国程序员成长平台

标题: ios - 为什么我有时会从 CLLocationManager 获得不准确的纬度和经度值? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 12:32
标题: ios - 为什么我有时会从 CLLocationManager 获得不准确的纬度和经度值?

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;


if ([self.locationManager respondsToSelectorselector(requestAlwaysAuthorization)])
{
    [self.locationManager requestAlwaysAuthorization];

}
[locationManager startUpdatingLocation];

- (void)locationManagerCLLocationManager *)manager didUpdateToLocationCLLocation *)newLocation fromLocationCLLocation *)oldLocation
{

    if(newLocation.horizontalAccuracy<0)
    {
        [locationManager startUpdatingLocation];
    }
    else
    {
        NSTimeInterval interval = [newLocation.timestamp timeIntervalSinceNow];

        if(abs(interval)<15)
        {
//Here i am doing my code but i get location which is far from my current location last time maximum distance i got from the current location was near about 3000 meter

            [locationManager stopUpdatingLocation];
        }
    }
}

我使用此代码,但有时无法获得准确的位置,因为它给出的距离当前位置超过 1 公里 我想要准确的位置



Best Answer-推荐答案


当您第一次请求位置更新时,您可能会获得上次 GPS 处于事件状态时的“陈旧”位置。 (我见过几公里外的陈旧位置读数。)前几个位置的准确性也往往很差。

您应该检查您获得的位置上的日期戳,并拒绝任何超过 1 秒的历史,并拒绝那些准确度读数大于您所需准确度的人。

编辑:

您的 didUpdateToLocation 方法没有意义。

当你调用startUpdatingLocation时,你会随着位置的变化而得到位置更新,直到你调用stopUpdatingLocation

没有理由在 didUpdateToLocation 方法中调用 startUpdatingLocation,因为位置已经在更新。事实上,它可能会把事情搞砸。不要那样做。

在伪代码中,你想做的是这样的:

- (void)locationManagerCLLocationManager *)manager didUpdateToLocationCLLocation *)newLocation fromLocationCLLocation *)oldLocation
{
  if the time of the update is > 5 seconds old, return.

  if horizontalAccuracy < 0 || horizontalAccuracy > 500, return. 

  stop updating location.
  
  do whatever you want to do with the location.
}

当您不做任何事情返回时,随着 GPS 稳定下来,您将获得更多位置更新。

我以 500 作为可接受的最大准确度读数为例。那将是 0.5 公里,这是一个很大的错误。较小的数字(例如 100 或 50)会产生更好的结果,但需要更长的时间。

关于ios - 为什么我有时会从 CLLocationManager 获得不准确的纬度和经度值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41142971/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4