7

実行をマップするのと同様に、ルートを構築して保存するアプリケーションを構築しようとしています。私はBreadcrumbサンプル コードを使用しています。具体的にCrumbPathCrumbPathView、ルートのベースとして Apple から提供されています。2 つの質問:

  1. のようなMKMapPoint *pointsオブジェクトにアクセスしようとすると:CrumbPath

    [_route lockForReading];
    NSLog(@"%@", _route.points);
    NSLog(@"%d", _route.pointCount);
    [_route unlockForReading];
    

    次のように言って、アプリがクラッシュします。

    Thread 1: EXC_BAD_ACCESS (code: 1, address: 0x9450342d)
    

    CrumbPath.mファイル内で、アップルの人々は明示的に書き込みロックを取得してからロックを解除することで「配列」に書き込みますが、読み取りロックを取得してそこから読み取ろうとすると、それを理解するのに苦労します。クラッシュします。

  2. にアクセスしようとする理由pointsは、 を取得してオブジェクトにMKMapPoints変換し、保存して、ユーザーの要求に応じて を再描画できるようにするためです。にアクセスできないため、 からに送信したオブジェクトを配列に保存して、 Parseバックエンドにアップロードしようとしましたが、常に次のようなエラーが表示されます。CLLocationCoordinate2DpolylinepointsCLLocationCoordinate2DlocationManager_route

    Sending 'CLLocationCoordinate2D' to parameter of incompatible type 'id'
    

    これはこれを簡単にするものではありません。これらのエラーが発生する理由について誰かが洞察を持っていますか?

ロケーションマネージャーデリゲート

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    if (_userLocation.longitude != manager.location.coordinate.longitude
        && _userLocation.latitude != manager.location.coordinate.latitude) {
        _userLocation = manager.location.coordinate;
    }

    if (_isRecording) {
        if (!_route) {
            NSLog(@"lat: %f, lng: %f", _userLocation.latitude, _userLocation.longitude);
            _route = [[CrumbPath alloc] initWithCenterCoordinate:_userLocation];
            [_mapView addOverlay:_route];

            MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(_userLocation, 2000, 2000);
            [_mapView setRegion:region animated:YES];
        }else {
            MKMapRect updateRect = [_route addCoordinate:_userLocation];

            if (!MKMapRectIsNull(updateRect)) {
                MKZoomScale currentZoomScale = (CGFloat)(_mapView.bounds.size.width / _mapView.visibleMapRect.size.width);
                CGFloat lineWidth = MKRoadWidthAtZoomScale(currentZoomScale);
                updateRect = MKMapRectInset(updateRect, -lineWidth, -lineWidth);
                [_routeView setNeedsDisplayInMapRect:updateRect];
            }
        }
        [_routePoints addObject:_userLocation];

        [_route lockForReading];
        NSLog(@"%d", _route.pointCount);
        NSLog(@"%@", _route.points);
        [_route unlockForReading];
    }
}

ロジックの記録を停止

    //stop recording
    NSLog(@"STOP");
    if (_route) {
        NSLog(@"There is a route");
        //Show route options toolbar
        [_route lockForReading];

        NSLog(@"%@", _route);
        NSLog(@"%d", _route.pointCount);
        NSLog(@"%@", _route.points);

        PFObject *routeToSave = [PFObject objectWithClassName:@"Routes"];
        //[routeToSave setObject:_route forKey:@"routePoints"];

        [_route unlockForReading];

        [routeToSave saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (!error) {
                NSLog(@"%c", succeeded);
            }else {
                NSLog(@"%@", error);
            }
        }];
    }
4

3 に答える 3

10

最初の問題に関して、クラッシュの原因は次のとおりです。

NSLog(@"%@", _route.pointCount);

そのはず:

NSLog(@"%d", _route.pointCount);

私のコメントで述べたように、%dカウントに使用する必要が%@あり、クラッシュが発生します。

2 番目の問題については、 ac 構造体をNSArray. NSValue配列に追加する前にラップする必要があります。CLLocationCoordinate2Dc構造体です。ここでドキュメントを確認してください。

これを変える:

[_routePoints addObject:_userLocation];

に:

NSValue *aValue = [NSValue valueWithMKCoordinate:_userLocation];
[_routePoints addObject:aValue];

から座標を取得するにはNSValue、次を使用できます。

[aValue MKCoordinateValue];

CLLocationCoordinate2Dエラー メッセージに記載されているように、オブジェクトを期待する配列に追加しようとしていました。

于 2013-01-14T23:00:46.110 に答える
1

解析するために使用している API は、任意のオブジェクトへのポインターである ID を期待しています。私が間違っていなければ、cllocationcoordinate2d は 2 つの double の c-struct であり、オブジェクトではありません。おそらく、小さなラッパー オブジェクトを作成して、これら 2 つの double を保存し、CLLocationCoordinate2d アイテムとの間で変換する必要があります。

于 2013-01-14T22:52:06.927 に答える
0

1:

行: NSLog(@"%@", _route.points); 間違っている

_route.points は文字列ではなく、NSStrig フォーマット記号 "%@" を使用しています。

さらに遠く:

CLLocationCoordinate2D は C-Sruct であり、Objective-C オブジェクトではないため、おそらく独自の GeoPoint クラスを作成する必要があります。

于 2013-01-14T22:59:59.433 に答える