【发布时间】:2013-09-03 08:15:49
【问题描述】:
我正在开发这个步行/跑步应用程序,它以米为单位计算距离并记录经纬度以供进一步使用。现在,当我计算距离时,我每次都会得到不正确的距离。我已经将它与其他正在运行的应用程序进行了比较,它们通常显示的距离与我的距离不同。
这是我正在使用的代码:
#define kDesiredAccuracy 5.0f
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
self.locationManager.distanceFilter = kDesiredAccuracy;
_routes = [[NSMutableArray alloc] init];
lastKnownLocation = nil;
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// return if accuracy is less than 0 or is greater than desired accuracy.
if (newLocation.horizontalAccuracy < 0)
{
return;
}
if(newLocation.horizontalAccuracy > kDesiredAccuracy)
{
return;
}
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
CLLocationSpeed speed = [newLocation speed];
// return if stale data or user is not moving
if (locationAge > 5.0 || speed <= 0) return;
//return if first location is found
if(lastKnownLocation == nil)
{
lastKnownLocation = newLocation;
return;
}
CLLocationDistance distance = [newLocation distanceFromLocation:(self.pramDistance > 0)?lastKnownLocation:oldLocation];
if(distance > 0)
{
// save distance for future use
NSMutableDictionary *dict=[[NSMutableDictionary alloc] init];
[dict setObject:[NSString stringWithFormat:@"%g", newLocation.coordinate.latitude] forKey:@"latitude"];
[dict setObject:[NSString stringWithFormat:@"%g", newLocation.coordinate.longitude] forKey:@"longtitude"];
[dict setObject:[NSString stringWithFormat:@"%f",distance] forKey:@"distance"];
[_routes addObject:dict];
// add distance to total distance.
self.pramDistance += distance;
}
}
一旦用户完成步行/跑步,我会在地图视图上绘制步行/跑步路线。为此,我只需使用所有记录的位置在 MKMapView 上绘制一条策略线。
地图视图显示路线和距离的曲折线总是不正确的。请建议我哪里做错了,我应该修改什么以使其正常工作?
这是比较(左边是别人的应用,右边是我的):
【问题讨论】:
-
目前我看到的界面还不够吸引我使用你的应用程序。而是你现在才发现,然后在你辛勤工作的几个月没有活动之后。
-
你只是在计算“乌鸦飞”的距离吗?
-
@CarlVeazey 我只需要记录总距离和位置以在地图上显示。
-
那么您实际上是在测量单条腿吗?还是只是直线距离?我不明白你的变量命名,所以不完全确定发生了什么。
-
@CarlVeazey 我实际上正在测量每一步,并希望它是最准确的。
标签: iphone ios objective-c xcode