gpt4 book ai didi

iphone - CLLocation 方法 distanceFromLocation : Inaccurate results 出现问题

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

我正在尝试使用 distanceFromLocation: 方法来计算我手持 iPhone 行走的总距离。到目前为止,我一直在四处寻找以帮助纠正我令人困惑、不准确且看似武断的结果。在这些代码片段中,theLabel 只是一个出现在我的应用程序界面中的标签对象,distanceMoved 是我试图存储我走过的总距离的变量,而 locMan 是一个在我的@interface 文件中声明的位置管理器.

- (void)viewDidLoad
{
locMan = [[CLLocationManager alloc] init];
locMan.delegate = self;
[locMan startUpdatingLocation];
isInitial = true;
distanceMoved = 0.0;
[super viewDidLoad];
}

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
distanceMoved += [newLocation distanceFromLocation: oldLocation];
theLabel.text = [NSString stringWithFormat: @"%f meters", distanceMoved];
}

如果您能帮助解决我在这里做错的问题,我们将不胜感激。谢谢!

最佳答案

  1. 过滤掉缓存的(旧的)位置数据,
  2. 过滤掉无效的定位结果(准确度 < 0),
  3. 将精度要求集设置为 kCLLocationAccuracyBest,
  4. 设置一个最小距离过滤器,最好至少为 10 米,以滤除位置噪音。
  5. 编辑:当oldLocation 为零时不计算距离。

您还可以做更多事情,但只要您获得足够好的 horizo​​nalAccuracy (<30m),这应该可行。您可以过滤掉低准确度的结果,但您必须自己跟踪 oldLocation,这有点复杂。

添加一个 NSLog(见下文)以便您可以了解发生了什么。如果您仍然没有得到好的结果,请发布 NSLog 的输出,以便我们了解发生了什么并提供更多帮助。

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSTimeInterval age = -[newLocation.timestamp timeIntervalSinceNow];

if (age > 120) return; // ignore old (cached) updates

if (newLocation.horizontalAccuracy < 0) return; // ignore invalid udpates

// EDIT: need a valid oldLocation to be able to compute distance
if (oldLocation == nil || oldLocation.horizontalAccuracy < 0) return;

CLLocationDistance distance = [newLocation distanceFromLocation: oldLocation];

NSLog(@"%6.6f/%6.6f to %6.6f/%6.6f for %2.0fm, accuracy +/-%2.0fm",
oldLocation.coordinate.latitude,
oldLocation.coordinate.longitude,
newLocation.coordinate.latitude,
newLocation.coordinate.longitude,
distance,
newLocation.horizontalAccuracy);

distanceMoved += distance;
theLabel.text = [NSString stringWithFormat: @"%f meters", distanceMoved];
}

- (void)viewDidLoad
{
locMan = [[CLLocationManager alloc] init];
locMan.delegate = self;
locMan.desiredAccuracy = kCLLocationAccuracyBest;
locMan.distanceFilter = 10;

[locMan startUpdatingLocation];
isInitial = true;
distanceMoved = 0.0;
[super viewDidLoad];
}

编辑 iOS6 如果您想使用 didUpdateLocations: 做同样的事情(因为现在不推荐使用上述方法)最简单的解决方案是简单地将每个位置保存在属性中,以便在下一次更新时您拥有以前的位置。在类中声明一个属性来保存 oldLocation:

@property (nonatomic, retain) CLLocation* oldLocation;

然后在委托(delegate)方法中保存每个 newLocation 以用作下次调用委托(delegate)方法时的 oldLocation:

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocations:(NSArray *)locations
{
CLLocation* newLocation = [locations lastObject];

NSTimeInterval age = -[newLocation.timestamp timeIntervalSinceNow];

if (age > 120) return; // ignore old (cached) updates

if (newLocation.horizontalAccuracy < 0) return; // ignore invalid udpates

// EDIT: need a valid oldLocation to be able to compute distance
if (self.oldLocation == nil || self.oldLocation.horizontalAccuracy < 0) {
self.oldLocation = newLocation;
return;
}

CLLocationDistance distance = [newLocation distanceFromLocation: self.oldLocation];

NSLog(@"%6.6f/%6.6f to %6.6f/%6.6f for %2.0fm, accuracy +/-%2.0fm",
self.oldLocation.coordinate.latitude,
self.oldLocation.coordinate.longitude,
newLocation.coordinate.latitude,
newLocation.coordinate.longitude,
distance,
newLocation.horizontalAccuracy);

distanceMoved += distance;
theLabel.text = [NSString stringWithFormat: @"%f meters", distanceMoved];

self.oldLocation = newLocation; // save newLocation for next time
}

关于iphone - CLLocation 方法 distanceFromLocation : Inaccurate results 出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10184802/

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