简体   繁体   English

行驶距离值不正确

[英]Inaccurate distance travelled values

New to using the corelocation framework could use some help understanding why my code isnt working how I'd like it too. 使用corelocation框架的新手可以帮助理解为什么我的代码无法正常工作。 I want it to track the distance travelled but when the view fires up it does different things everytime. 我希望它跟踪行进的距离,但是当视图启动时,它每次都会执行不同的操作。 Sometimes itll jump to 1000 or another large value or itll increase by random increments. 有时,它会跳到1000或另一个较大的值,或者会以随机增量增加。 Can anyone see why this code isnt working for me? 谁能看到为什么此代码对我不起作用?

    - (void)viewDidLoad
{
    locationManager = [[CLLocationManager alloc] init];
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.delegate = self;
    locationManager.distanceFilter = 100; //update every 100 meters
    [locationManager startUpdatingLocation];
    startLocation = nil;

    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    if (startLocation == nil)
    {
        totalDistanceBetween = 0;
        self.startLocation = newLocation;
    }
    CLLocationDistance distanceBetween = [newLocation distanceFromLocation:startLocation ];
    startLocation = newLocation;
    totalDistanceBetween += distanceBetween;
    totalDistanceBetween = totalDistanceBetween * 0.001; //convert to m value to km
    NSString *tripString = [[NSString alloc]
                            initWithFormat:@"%.02f",
                            totalDistanceBetween];
    distance.text = tripString;
}

The first thing that jumps out to me is that you set startLocation to nil after you tell the location manager to start giving you updates. 让我startLocation的第一件事是,您告诉位置管理器开始提供更新 ,将startLocation设置为nil

Depending on how startLocation is defined/initialized/reused, it might not contain the initial value you expect it to. 根据startLocation的定义/初始化/重用方式,它可能不包含您期望的初始值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 累计行驶距离 SwiftUI - Cumulative distance travelled SwiftUI CoreLocation查找行进的距离并用作变量iOS - CoreLocation find distance travelled and use as variable iOS 确定行进路径 mkmapview swift 上的距离 - Determine distance on travelled path mkmapview swift CLLocationManager或GoogleMap-IOS跟踪用户行驶的距离 - Track distance travelled by user by CLLocationManager or GoogleMap-IOS 测量两个位置之间的距离不准确 - Measure distance between two location inaccurate Spritekit-计算手指在两个事件之间的移动距离,以增加NSUInteger - Spritekit - calculate distance the finger has travelled between two events to increment your a NSUInteger 如何使用M7协处理器计算iOS设备中的行进距离,速度,持续时间? - How to calculate distance travelled , speed , duration in iOS devices with M7 Co-processor? 删除行进的GMSPolyline段 - Remove travelled GMSPolyline segment 滑块不正确 - Slider Inaccurate 对象框架在运行时会缩放,但会保留恒定值,从而以编程方式难以/不准确地放置项目 - Object frames scale at runtime, but retain constant values, making placing items programmatically difficult/inaccurate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM