我正在使用AFNetworking
,并且我正在通过从谷歌方向获取方向来关注关于在 iphone 屏幕上绘制路线的教程。我正在使用JSON
, 和AFNetworking
. 我从教程中复制了代码,您可以在此处找到:教程
如果您还选择复制和测试此代码,请注意:您需要AFNetworking
来自此 github 页面:AFNetworking 下载
您还必须自己在 .h 中将变量定义_path
为 an NSMutableArray
,否则您将收到错误,因为他们没有定义它但引用了它。
这是代码:
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
你们能帮忙吗?谢谢!