Corelocation错误的距离 [英] Corelocation incorrect distances

查看:118
本文介绍了Corelocation错误的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个计算用户行进距离的应用程序。我正在使用CLLocationManager类来执行此操作,但我最初获取缓存数据,并且距离变量也以突然的速率增加。请帮帮我......我使用了以下代码....

I am developing an application which computes the distance traveled by the user. I am using CLLocationManager class to do so, but I am getting the cached data initially and also the distance variable is increasing at a sudden rate. Please help me out... I have used the following code....

注意:距离是静态变量。这里

Note:distance is a static var. here

  - (void)viewDidLoad {
    [super viewDidLoad];
//bestEffortAtLocation = nil;
oldLocat = [[CLLocation alloc]init];
newLocat = [[CLLocation alloc]init];
locationManager =[[CLLocationManager alloc]init];
locationManager.delegate = self;
locationManager.distanceFilter =  kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];

  }

- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation{




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


NSLog(@"accuracy %d",newLocation.horizontalAccuracy);

// 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];
//NSLog(@"time %d",locationAge);

if (locationAge > 5.0) return;



self.oldLocat = oldLocation;
self.newLocat = newLocation;


double latDegrees = newLocation.coordinate.latitude;
NSString *lat = [NSString stringWithFormat:@"%1.5f°",latDegrees];
latLabel.text = lat;
double longDegrees = newLocation.coordinate.longitude;
NSString *longt = [NSString stringWithFormat:@"%1.5f°",longDegrees];
longLabel.text = longt;
[self computeDistanceFrom:oldLocat tO:newLocat];

   }

    -(void)computeDistanceFrom:(CLLocation *)oldL tO:(CLLocation *)newL
 {
NSLog(@"oldd %@",oldL);
NSLog(@"new %@",newL);


distance = distance + [oldL getDistanceFrom:newL];
NSLog(@"distance %f",distance);

}

控制台显示以下数据..... ..

The console is displaying the following data.......


准确度0 oldd(null)new< +28.62114850,+ 77.37001021> +/- 80.00m
(速度-1.00 mps / course -1.00)@ 2010-06-22 19:21:59 +0530距离
0.000000

accuracy 0 oldd (null) new <+28.62114850, +77.37001021> +/- 80.00m (speed -1.00 mps / course -1.00) @ 2010-06-22 19:21:59 +0530 distance 0.000000

准确度0 oldd< +28.62114850, +77.37001021> +/- 80.00m(速度-1.00
mps / course -1.00)@ 2010-06-22 19:21:59 +0530 new< +28.61670485,
+77.37068155> + / - 80.00米(速度-1.00 mps /航向-1.00)@ 2010-06-22 19:22:00 +0530
距离498.211345

accuracy 0 oldd <+28.62114850, +77.37001021> +/- 80.00m (speed -1.00 mps / course -1.00) @ 2010-06-22 19:21:59 +0530 new <+28.61670485, +77.37068155> +/- 80.00m (speed -1.00 mps / course -1.00) @ 2010-06-22 19:22:00 +0530 distance 498.211345

准确度0 oldd < +28.61670485,+ 77.37068155> +/- 80.00m(速度-1.00
mps / course -1.00)@ 2010-06-22 19:22:00 +0530 new< +28.62112748,
+77.36998540> +/- 80.00m(速度-1.00 mps /航向-1.00)@ 2010-06-22 19:23:02 +0530距离994.432508

accuracy 0 oldd <+28.61670485, +77.37068155> +/- 80.00m (speed -1.00 mps / course -1.00) @ 2010-06-22 19:22:00 +0530 new <+28.62112748, +77.36998540> +/- 80.00m (speed -1.00 mps / course -1.00) @ 2010-06-22 19:23:02 +0530 distance 994.432508


推荐答案

最初获得缓存l是正常的从以前开始。您可以通过查看 timestamp rel =noreferrer> CLLocation 。

It is normal to initially get a cached location from before. You can ignore older cached data by looking at the timestamp of the CLLocation.

您输入的准确度不正确,使用%f而不是%d,输入为double而不是int。

You are printing the accuracy incorrectly, use %f not %d, type is double not int.

GPS首次启动时位置可以快速变化,因为您从单元格三角测量中获得的精度较低,然后当您获得GPS采集时,您将获得更高精度的位置。那些距离很远(1000米),看起来你在几秒钟内移动了很远,但只是准确性发生了变化。

Location can change quickly when GPS first starts because you have a low accuracy location from cell triangulation, then as you get GPS acquisition you get a higher accuracy location. Those can be far apart (1000m) and it appears that you moved far in a few seconds but only the accuracy has changed.

不要使用两个精度差异很大的位置来计算行进距离。

Don't use two locations that have very different accuracy for computing distance traveled.

编辑添加了代码示例,如何忽略旧位置数据。您决定要忽略多久,我在这里使用了60秒:

EDIT Added code sample, how to ignore old location data. You decide how old to ignore, I used 60 seconds here:

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
           fromLocation:(CLLocation *)oldLocation {

    NSTimeInterval ageInSeconds = -[newLocation.timestamp timeIntervalSinceNow];
    if (ageInSeconds > 60.0) return;   // data is too long ago, don't use it

    // process newLocation
    ...
}

这篇关于Corelocation错误的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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