objective-c – Corelocation错误的距离
我正在开发一个计算用户行进距离的应用程序.我正在使用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); } 控制台显示以下数据…….
解决方法
从以前开始获取缓存位置是正常的.您可以通过查看
CLLocation的
timestamp来忽略较旧的缓存数据.
您打印精度不正确,使用%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 ... } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |