Google Directions iOS API を使用しています。XML ではなく JSON を使用してデータを取得しています。しかし、私はAFNetworking
これを単純化するために使用しています。AFNetworking
github で入手できます。現在、ある場所から別の場所へのオーバーレイ ルートを に表示できます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[]
。そしてもちろん有効です。それがルートを機能させた方法です。NULL
bounds
overview_polyline
では、ここで何が問題なのですか?NULL
必要なときにGoogle Directions API が提供するオブジェクトの数が非常に多いのはなぜですか?
ありがとう!