0

Google Directions iOS API を使用しています。XML ではなく JSON を使用してデータを取得しています。しかし、私はAFNetworkingこれを単純化するために使用しています。AFNetworkinggithub で入手できます。現在、ある場所から別の場所へのオーバーレイ ルートを に表示できますMKMapView。これが私のコードです:

// AFNETWORKING ==========================================================

        AFHTTPClient *_httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://maps.googleapis.com/"]];

    [_httpClient registerHTTPOperationClass: [AFJSONRequestOperation class]];

    [_httpClient setDefaultHeader:@"Accept" value:@"application/json"];

    NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];

    [parameters setObject:[NSString stringWithFormat:@"%f,%f", location.coordinate.latitude, location.coordinate.longitude] forKey:@"origin"];

    [parameters setObject:[NSString stringWithFormat:@"%f,%f", location2.coordinate.latitude, location2.coordinate.longitude] forKey:@"destination"];

    [parameters setObject:@"false" forKey:@"sensor"];

    [parameters setObject:@"driving" forKey:@"mode"];

    [parameters setObject:@"metric" forKey: @"units"];

    NSMutableURLRequest *request = [_httpClient requestWithMethod:@"GET" path: @"maps/api/directions/json" parameters:parameters];

    request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;

    AFHTTPRequestOperation *operation = [_httpClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSInteger statusCode = operation.response.statusCode;

        if (statusCode == 200) {

            [self parseResponse:responseObject];

        } else {

        }

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { }];

    [_httpClient enqueueHTTPRequestOperation:operation];

// ROUTE SETUP AND RESPONSE RECIEVED METHOD ==========================================================


- (void)parseResponse:(NSDictionary *)response {

NSArray *routes = [response objectForKey:@"routes"];

NSDictionary *routePath = [routes lastObject];

if (routePath) {

    NSString *overviewPolyline = [[routePath objectForKey: @"overview_polyline"] objectForKey:@"points"];

    NSLog(@"Status: %@", [response objectForKey: @"status"]);

    NSLog(@"Legs: %@", [routePath objectForKey: @"legs[]"]);

    _path = [self decodePolyLine:overviewPolyline];

    NSInteger numberOfSteps = _path.count;

    CLLocationCoordinate2D coordinates[numberOfSteps];
    for (NSInteger index = 0; index < numberOfSteps; index++) {
        CLLocation *location = [_path objectAtIndex:index];
        CLLocationCoordinate2D coordinate = location.coordinate;

        coordinates[index] = coordinate;
    }

    polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
    [self.mapView addOverlay:polyLine];
}

}

これは、ルートを機能させるためのメイン コードです。そして、それはうまくいきます!しかし、今私がやりたいことは、方向リストと完全な期間と要約を取得することです。そこで、Google Directions API のドキュメントを詳しく調べたところ、さまざまな辞書、配列、およびオブジェクトを使用するように指示されました。しかし、私は運がありませんでした。OK のステータスを取得します。配列のカウントをログに記録するとroutes、そのオブジェクトは 1 つだけです。次に、最も重要なlegs[]配列はNULL.

    NSLog(@"Legs is: %@", [routePath objectForKey: @"legs[]"]);

出力:

脚は (null)

Legs[]方向リストや期間などの重要なすべてが含まれています。SummaryではないNULL場合、ルートが迂回する通りの 1 つの名前を示します。まとめ方がわかりません。routes配列にオブジェクトが 1 つしかないことが原因である可能性があります。waypoint_orderそしてあまりにもですwarnings[]。そしてもちろん有効です。それがルートを機能させた方法です。NULLboundsoverview_polyline

では、ここで何が問題なのですか?NULL必要なときにGoogle Directions API が提供するオブジェクトの数が非常に多いのはなぜですか?

ありがとう!

4

1 に答える 1

0

了解しました。問題は、指示に対する応答に時間がかかりすぎることでした。legs[]と一緒にすぐに配信される応答を期待していましたoverview_polyline。そのため、応答が届くまで待つ必要がありました。これにより、バッファーUIを挿入する機会が得られます。

于 2012-08-07T11:59:12.163 に答える