0

私の iOS アプリでは、始点から現在地までの距離を追跡する必要があります。このコードを実装しました:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    CLLocation *currentLocation = [locations lastObject];
    CLLocation *startLocation = [locations firstObject];
    [coordArray addObject:currentLocation];
    float speed = currentLocation.speed * 3.6;
    if (speed > 0) {
        self.labelSpeed.text = [NSString stringWithFormat:@"%.2f Km/h", speed];
        [speedArray addObject:[NSNumber numberWithFloat:speed]];
    }
    CLLocationDistance distance = [startLocation distanceFromLocation:currentLocation];
}

しかし、アプリを使用しようとすると、距離が取れません。ラベルに表示するには距離が必要です。距離を使用して、次の式を使用してステップ数を計算します。

steps = distance / length of human step

正確ではないことは承知していますが、iPhoneのディスプレイがアクティブでない間は動作しないため、加速度計を使用できません。人が私にこの解決策を提案しました。コードで距離が表示されないのはなぜですか?

4

2 に答える 2

0

距離を取得するには、以下のコードを確認してください。

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {


    if ([coordArray count]>0) {

        CLLocation *currentLocation = manager.location;
        CLLocation *startLocation = [coordArray objectAtIndex:0];
        [coordArray addObject:currentLocation];
        float speed = currentLocation.speed * 3.6;
        if (speed > 0) {
            NSLog(@"\n speed:%@",[NSString stringWithFormat:@"%.2f Km/h", speed]);
        }

        CLLocationDistance distance = [startLocation distanceFromLocation:currentLocation];

        if (distance>0) {
            NSLog(@"Distance:%@",[NSString stringWithFormat:@"%lf meters",distance]);
        }

    }
    else{
        [coordArray addObject:manager.location];
    }
}
于 2014-03-03T10:27:47.330 に答える