我正在开发这个步行/跑步应用程序,它以米为单位计算距离并记录经纬度以供进一步使用。现在,当我计算距离时,我每次都会得到不正确的距离。我已经将它与其他正在运行的应用程序进行了比较,它们通常显示的距离与我的距离不同。
这是我正在使用的代码:
#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 上绘制一条策略线。
地图视图显示路线和距离的曲折线总是不正确的。请建议我在哪里做错了,我应该修改什么以使其正常工作?
这是比较(左一个是别人的应用程序,右一个是我的):