1

使用 corelocation 框架的新手可以使用一些帮助来理解为什么我的代码也不能按照我想要的方式工作。我希望它跟踪行进的距离,但是当视图启动时,它每次都会做不同的事情。有时它会跳到 1000 或另一个大值,或者它会随机增加。谁能明白为什么这段代码对我不起作用?

    - (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;
}
4

1 回答 1

1

跳出来的第一件事是你在告诉位置经理开始startLocation给你更新nil 之后设置。

根据startLocation定义/初始化/重用的方式,它可能不包含您期望的初始值。

于 2013-10-22T19:00:23.187 回答