Objective c 取芯位置不正确的距离
我正在开发一个应用程序来计算用户所走的距离。我使用CLLocationManager类来实现这一点,但我最初得到的是缓存数据,距离变量也在以突然的速度增加。请帮帮我。。。我使用了以下代码 注:此处距离是一个静态变量Objective c 取芯位置不正确的距离,objective-c,iphone,xcode,cllocationmanager,core-location,Objective C,Iphone,Xcode,Cllocationmanager,Core Location,我正在开发一个应用程序来计算用户所走的距离。我使用CLLocationManager类来实现这一点,但我最初得到的是缓存数据,距离变量也在以突然的速度增加。请帮帮我。。。我使用了以下代码 注:此处距离是一个静态变量 - (void)viewDidLoad { [super viewDidLoad]; //bestEffortAtLocation = nil; oldLocat = [[CLLocation alloc]init]; newLocat = [[CLLocation al
- (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);
}
-(void)viewDidLoad{
[超级视图下载];
//bestEffortAtLocation=零;
oldLocat=[[CLLocation alloc]init];
newLocat=[[CLLocation alloc]init];
locationManager=[[CLLocationManager alloc]init];
locationManager.delegate=self;
locationManager.distanceFilter=KCLDistanceFilterOne;
locationManager.desiredAccuracy=KCallocationAccuracyBest;
[locationManager startUpdatingLocation];
}
-(无效)locationManager:(CLLocationManager*)经理
DidUpdateLocation:(CLLocation*)newLocation
fromLocation:(CLLocation*)oldLocation{
//测试水平精度是否表明测量无效
if(newLocation.horizontalcreaction<0)返回;
NSLog(@“精度%d”,新位置。水平精度);
//测试位置度量的年龄,以确定度量是否已缓存
//在大多数情况下,您不希望依赖缓存的度量
NSTimeInterval locationAge=-[newLocation.timestamp timeintervalncesInnow];
//NSLog(@“时间%d”,位置);
如果(位置>5.0)返回;
self.oldLocat=oldLocation;
self.newLocat=新位置;
双纬度=newLocation.coordinate.latitude;
NSString*lat=[NSString stringWithFormat:@“%1.5f°”,lat度];
latLabel.text=lat;
double longDegrees=newLocation.coordinate.longitude;
NSString*longt=[NSString stringWithFormat:@“%1.5f°”,longDegrees];
longLabel.text=long;
[自计算距离从:旧LOCAT到:新LOCAT];
}
-(void)计算距离从:(CLLocation*)oldL到:(CLLocation*)newL
{
NSLog(@“old%@”,oldL);
NSLog(@“新%@”,newL);
距离=距离+[oldL getDistanceFrom:newL];
NSLog(@“距离%f”,距离);
}
控制台正在显示以下数据
精度0旧(零)新+/-80.00m
(速度-1.00mps/航程-1.00)@2010-06-22 19:21:59+0530距离
百万
精度0-D+/-80.00m(速度-1.00
2010-06-22 19:21:59+0530新里程+/-80.00米(速度-1.00英里/航向-1.00)@2010-06-22 19:22:00+0530
距离498.211345
精度0-D+/-80.00m(速度-1.00
mps/航向-1.00)@2010-06-22 19:22:00+0530新建+/-80.00米(速度-1.00 mps/航向-1.00)@2010-06-22 19:23:02+0530距离994.432508
最初从以前获取缓存位置是正常的。您可以通过查看的来忽略较旧的缓存数据 打印精度不正确,请使用%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
...
}
您已经在确保位置更新的时间少于5秒。这就是这行代码的作用:
if (locationAge > 5.0) return;
正如progrmr所述,这里的关键问题几乎可以肯定的是,您依赖于对位置的初始估计,而这些估计的准确性较低,因此,当您的位置得到更好的定位时,位置似乎移动得很快。您需要做的是,首先确保只有在获得准确的初始位置修复时才设置oldLocation,然后仅在新位置具有可接受的分辨率时将其与该oldLocation进行比较
例如,您可以有如下内容:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation{
// Ignore location updates that are less than 10m accuracy, or where the horizontalAccuracy < 0
// which indicates an invalid measurement.
NSLog(@"New location accuracy %.0fm", newLocation.horizontalAccuracy);
if ((newLocation.horizontalAccuracy < 0) || (newLocation.horizontalAccuracy > 10)) return;
// Ignore location updates that are more than five seconds old, to avoid responding to
// cached measurements.
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
if (locationAge > 5) return;
if (self.oldLocat == NULL)
{
// For the first acceptable measurement, simply set oldLocat and exit.
self.oldLocat = newLocation;
return;
}
// A second location has been identified. Calculate distance travelled.
// Do not set self.oldLocat from the oldLocation provided in this update, as we wish to
// use the last place we calculated distance from, not merely the last place that was
// provided in a location update.
CLLocationDistance distance = [newLocation distanceFromLocation:self.oldLocat];
NSLog(@"Distance: %.0fm", distance);
// This new location is now our old location.
self.oldLocat = newLocation;
}
-(无效)locationManager:(CLLocationManager*)manager
DidUpdateLocation:(CLLocation*)newLocation
fromLocation:(CLLocation*)oldLocation{
//忽略精度小于10m或水平精度小于0的位置更新
//这表示测量值无效。
NSLog(@“新定位精度%.0fm”,新定位精度.水平精度);
if((newLocation.horizontalcreaction<0)| |(newLocation.horizontalcreaction>10))返回;
//忽略超过5秒的位置更新,以避免响应
//缓存的度量值。
NSTimeInterval locationAge=-[newLocation.timestamp timeintervalncesInnow];
如果(位置>5)返回;
if(self.oldLocat==NULL)
{
//对于第一个可接受的测量,只需设置oldLocat和exit。
self.oldLocat=新位置;
返回;
}
//已确定第二个位置。计算行驶距离。
//不要像我们希望的那样,从本更新中提供的oldLocation设置self.oldLocat
//使用我们计算距离的最后一个地方,而不仅仅是
//在位置更新中提供。
CLLocationDistance=[newLocation distanceFromLocation:self.oldLocat];
NSLog(@“距离:%.0fm”,距离);
//这个新位置现在是我们的旧位置。
self.oldLocat=新位置;
}
注意:对于上面的代码,您不需要computeDistanceFrom:tO:,而且实际上也不需要self.newLocat属性,至少在这段代码中。谢谢progrmr,但你能告诉我如何使用时间戳删除缓存数据吗……。如果上述计算距离的方法是错误的,那么我应该如何继续……是否有其他方法来计算距离……因为我在App store中看到过许多类似的应用程序计算距离,速度等。请给我你的宝贵建议。我添加了一个代码样本到sh