私は を使用してAFNetworking
おり、Google のルート案内からルートを取得して iPhone 画面にルートを描画する方法についてのチュートリアルに従っています。JSON
、およびを使用してAFNetworking
います。ここにあるチュートリアルからコードをコピーしました:チュートリアル
このコードもコピーしてテストすることを選択した場合は、注意してください:AFNetworking
この github ページから必要です: AFNetworking ダウンロード
また、変数_path
をNSMutableArray
.h で自分で定義する必要があります。そうしないと、変数を定義していないが参照しているため、エラーが発生します。
コードは次のとおりです。
AFHTTPClient *_httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://maps.googleapis.com/"]];
[_httpClient registerHTTPOperationClass: [AFJSONRequestOperation class]];
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:@"true" forKey:@"sensor"];
NSMutableURLRequest *request = [_httpClient requestWithMethod:@"GET" path: @"maps/api/directions/json" parameters:parameters];
request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
AFHTTPRequestOperation *operation = [AFHTTPClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id response) {
NSInteger statusCode = operation.response.statusCode;
if (statusCode == 200) {
[self parseResponse:response];
} else {
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { }];
[_httpClient enqueueHTTPRequestOperation:operation];
だからここに私の問題があります
助けてくれた人に感謝します。エラーなしでコードをテストしましたが、ルートを作成しようとしているときにエラーが発生しました。ここでクラッシュします:
- (void)parseResponse:(NSDictionary *)response {
NSArray *routes = [response objectForKey:@"routes"]; // CRASH HERE
NSDictionary *routePath = [routes lastObject];
if (routePath) {
NSString *overviewPolyline = [[routePath objectForKey: @"overview_polyline"] objectForKey:@"points"];
_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;
}
MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
[self.mapView addOverlay:polyLine];
}
}
エラーの説明:
-[__NSCFData objectForKey:]: 認識されないセレクターがインスタンス 0x2004bb80 に送信されました
手伝ってくれませんか?ありがとう!