2

私のアプリでは足音を数えていますが、速度を知りたいので、GPS 座標を保存してポリラインを別の ViewController に描画したいと考えています。次のコードを使用して、この座標を保存できると思いました。

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        locationArray = [[NSMutableArray alloc]init];
        [locationArray addObject:currentLocation];
        speed = (int) currentLocation.speed * 3.6;
        self.labelSpeed.text = [NSString stringWithFormat:@"%d Km/h",speed];
        NSLog(@"%@", [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]);
        NSLog(@"%@", [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]);
    }
}

locationArrayしかし、私の場合、GPS センサーによって受信された最後の座標のみが保存されているため、機能しません。私が何を開発しようとしているのかをよりよく説明するために、ここに何か具体的なことを書きます: 最初の ViewController で、ステップ数と速度を計算する 2 つのラベルを表示したいと思います。したがって、この ViewController では、座標データを受け取る必要があり、このデータを に挿入する必要があると考えましたNSMutableArray。2 番目の ViewController では、(メソッドを使用して) 合計ステップ数を挿入するラベルとprepareForSegue、作成したパスを表示するポリラインを描画する MapView の下にラベルを表示します。そのためには、最初の ViewController で受け取った座標が必要なので、次を使用して最初の ViewController から 2 番目の ViewController にデータを渡す必要があります。prepareForSegue方法。私の問題は、ポリラインを描画するために、すべての座標を 2 番目の ViewController に格納する方法です。誰か助けてくれませんか?

ありがとうございました

4

1 に答える 1

1

新しい場所を取得するたびに配列を初期化し、割り当て行を別の方法 ( などviewDidLoad) に移動し、その行をdidUpdateToLocation.

- (void)viewDidLoad
{
    [super viewDidLoad];
    locationArray = [[NSMutableArray alloc]init];
    //.... more code
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        //locationArray = [[NSMutableArray alloc]init]; //This line doesn't go here
        [locationArray addObject:currentLocation];
        speed = (int) currentLocation.speed * 3.6;
        self.labelSpeed.text = [NSString stringWithFormat:@"%d Km/h",speed];
        NSLog(@"%@", [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]);
        NSLog(@"%@", [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]);
    }
}
于 2013-09-04T14:23:53.213 に答える