gpt4 book ai didi

iphone - CoreLocation 负值

转载 作者:搜寻专家 更新时间:2023-10-30 19:58:25 25 4
gpt4 key购买 nike

我正在使用 CoreLocation 框架获取我的速度和距离来计算平均速度。

CoreLocation 发出的第一个更新中,它显示了速度和行进距离的负值。我该如何解决这个问题?

速度是 locationController.locationManager.location.speed,其中 locationController 持有我的 CoreLocation 委托(delegate)。通过获取旧位置和新位置并计算距离来计算距离。

//Distance
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
// make sure the old and new coordinates are different
if ((oldLocation.coordinate.latitude != newLocation.coordinate.latitude) &&
(oldLocation.coordinate.longitude != newLocation.coordinate.longitude))
{
mDistance = [newLocation distanceFromLocation:oldLocation];
}
}

最佳答案

由于多种原因,Core Location 返回的数据可能无效。在使用数据之前,请运行此方法以查看它是否有效。

// From http://troybrant.net/blog/2010/02/detecting-bad-corelocation-data/
- (BOOL)isValidLocation:(CLLocation *)newLocation
withOldLocation:(CLLocation *)oldLocation
{
// filter out nil locations
if (!newLocation){
return NO;
}
// filter out points by invalid accuracy
if (newLocation.horizontalAccuracy < 0){
return NO;
}
// filter out points that are out of order
NSTimeInterval secondsSinceLastPoint = [newLocation.timestamp
timeIntervalSinceDate:oldLocation.timestamp];
if (secondsSinceLastPoint < 0){
return NO;
}
// filter out points created before the manager was initialized
NSTimeInterval secondsSinceManagerStarted = [newLocation.timestamp
timeIntervalSinceDate:locationManagerStartDate];
if (secondsSinceManagerStarted < 0){
return NO;
}
// newLocation is good to use
return YES;
}

关于iphone - CoreLocation 负值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7970664/

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