gpt4 book ai didi

objective-c - 核心定位不正确的距离

转载 作者:太空狗 更新时间:2023-10-30 03:59:54 26 4
gpt4 key购买 nike

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

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

  - (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);

}

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

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

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

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

最佳答案

初始获取之前的缓存位置是正常的。您可以通过查看 timestamp 来忽略旧的缓存数据。的 CLLocation .

您打印的准确性不正确,使用 %f 而不是 %d,类型是 double 而不是 int。

当 GPS 首次启动时,位置可能会快速变化,因为您从小区三角测量获得的位置精度较低,然后当您获取 GPS 时,您会获得更高精度的位置。那些可能相距很远(1000 米),看起来你在几秒钟内移动了很远,但只是精度发生了变化。

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

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

- (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
...
}

关于objective-c - 核心定位不正确的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3093919/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com