Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在我的步行/跑步应用程序中发现不正确的距离和路线_Iphone_Ios_Objective C_Xcode - Fatal编程技术网

Iphone 在我的步行/跑步应用程序中发现不正确的距离和路线

Iphone 在我的步行/跑步应用程序中发现不正确的距离和路线,iphone,ios,objective-c,xcode,Iphone,Ios,Objective C,Xcode,我正在开发这个步行/跑步应用程序,它以米为单位计算距离并记录lat long以供进一步使用。现在,当我计算距离时,每次都得到不正确的距离。我将它与其他跑步应用程序进行了比较,它们显示的距离通常与我的距离不同 以下是我正在使用的代码: #define kDesiredAccuracy 5.0f self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.desiredAccuracy = kCLLoc

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

以下是我正在使用的代码:

#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;
    }

}
#定义KDesiredAccurance 5.0f
self.locationManager=[[CLLocationManager alloc]init];
self.locationManager.desiredAccuracy=KCallocationAccuracybestforNavigation;
self.locationManager.distanceFilter=kdesiredAccurance;
_routes=[[NSMutableArray alloc]init];
LastKnown位置=零;
-(void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation
{
//如果精度小于0或大于所需精度,则返回。
if(newLocation.HorizontalAccurance<0)
{
返回;
}
if(newLocation.HorizontalAccurance>KDesiredAccurance)
{
返回;
}
NSTimeInterval locationAge=-[newLocation.timestamp timeintervalncesInnow];
CLLocationSpeed speed=[新位置速度];
//如果过时数据或用户未移动,则返回
如果(位置年龄>5.0 | |速度0)?最新已知位置:旧位置];
如果(距离>0)
{
//节省距离以备将来使用
NSMutableDictionary*dict=[[NSMutableDictionary alloc]init];
[dict setObject:[NSString stringWithFormat:@“%g”,newLocation.coordinate.latitude]forKey:@“latitude”];
[dict setObject:[NSString stringWithFormat:@“%g”,newLocation.coordinate.longitude]forKey:@“longtudent”];
[dict setObject:[NSString stringWithFormat:@“%f”,距离]forKey:@“距离”];
[_:dict];
//将距离添加到总距离。
self.pramDistance+=距离;
}
}
一旦用户完成步行/跑步,我将在地图视图上绘制步行/跑步路线。为此,我只需使用所有记录的位置在MKMapView上绘制一条策略线

地图视图显示路线和距离的之字形线始终不正确。请告诉我哪里做错了,我应该修改什么使其正常工作

下面是比较(左一个是别人的应用,右一个是我的): 试试这个

导入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];
    }

虽然我在这里没有得到多少帮助,但我在这个问题上找到了一些帮助和想法:

我为解决此问题所做的是:

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

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

感谢您的快速回复。但我需要经常更新位置,以跟踪覆盖的路线用户。所以阻止它不会解决我的目的。此外,我还需要以米为单位的距离。目前,我看到的界面不够吸引我,我不想使用你的应用程序。相反,你现在就知道了,然后在几个月的艰苦工作中没有任何活动。你只是在计算“乌鸦飞”的距离吗?@Carlvaezey我只需要记录总距离和位置,以便在地图上显示它们。那么,你真的在测量每条腿吗?还是直线距离?我不明白你的变量命名,所以我不能完全确定发生了什么。@Carlvaezey我实际上在测量每一步,希望它是最准确的。