Sid*_*th 4 iphone xcode objective-c core-location cllocationmanager
我正在开发一个计算用户行进距离的应用程序.我正在使用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);
}
Run Code Online (Sandbox Code Playgroud)
控制台显示以下数据.......
准确度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
准确度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 m(速度-1.00 mps/course -1.00)@ 2010-06-22 19:22:00 +0530距离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.00 m(速度-1.00 mps/course -1.00)@ 2010-06-22 19:23:02 +0530距离994.432508
从以前开始获取缓存位置是正常的.您可以通过查看忽略旧的缓存数据的时间戳的的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
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3229 次 |
| 最近记录: |