1

我正在开发这个步行/跑步应用程序,它以米为单位计算距离并记录经纬度以供进一步使用。现在,当我计算距离时,我每次都会得到不正确的距离。我已经将它与其他正在运行的应用程序进行了比较,它们通常显示的距离与我的距离不同。

这是我正在使用的代码:

#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 上绘制一条策略线。

地图视图显示路线和距离的曲折线总是不正确的。请建议我在哪里做错了,我应该修改什么以使其正常工作?

这是比较(左一个是别人的应用程序,右一个是我的): 其他应用程序和我的应用程序的比较

4

2 回答 2

1

试试这个,

导入 CLLocationManagerDelegate,

 CLLocation *currentLocation = self.currentLocation;
       float distanceMile = [currentLocation distanceFromLocation:[[CLLocation alloc] initWithLatitude:latitude longitude:longitude]]/1609.34;

    -(void)postCurrentLocationOfDevice
    {

        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.distanceFilter = kCLDistanceFilterNone;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        [self.locationManager startUpdatingLocation];
        self.locationManager.delegate = self;

    }
    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
       self.currentLocation = [locations objectAtIndex:0];
        [self.locationManager stopUpdatingLocation];

    }

    -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {
        self.currentLocation = newLocation;
        [self.locationManager stopUpdatingLocation];
    }
于 2013-09-03T09:07:44.440 回答
1

虽然我在这里没有得到太多帮助,但我在这个问题中找到了一些帮助和想法:Corelocation wrong distances

我为解决这个问题所做的是:

  • 我将最近的 5 个位置存储在一个数组中。
  • 一旦 5 个项目存储在数组中,我检查了最近的位置。
  • 在获得最近的位置后,我计算了最后一个最佳位置和存储在最后一个最佳位置的新位置之间的距离。

PS:我的代码很乱,命名约定也不是那么通用,这就是为什么我没有在这里发布我的代码。

于 2013-09-05T16:07:26.657 回答