gpt4 book ai didi

iphone - map View 崩溃时的 GDirections 路线

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:20:42 26 4
gpt4 key购买 nike

我正在使用 AFNetworking,并且正在学习有关通过从 google 路线获取路线在 iphone 屏幕上绘制路线的教程。我正在使用 JSONAFNetworking。我从教程中复制了代码,您可以在这里找到:Tutorial

如果您还选择复制和测试此代码,请注意:您需要来自此 github 页面的 AFNetworking:AFNetworking Download

您还必须自己在 .h 中将变量 _path 定义为 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:]: unrecognized selector sent to instance 0x2004bb80

你们能帮忙吗?谢谢!

最佳答案

Let me also say that the AFHTTPClient previously did not have this HTTPOperationWithRequest class method as shown, but I had to copy and paste it in from AFHTTPRequestOperation.

我从您的 github 链接中克隆了 AFNetworking,并在 AFHTTPClient.h 文件中找到了它:

- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)request 
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

我想知道为什么您收到错误而不是“找不到方法”警告。 HTTPRequestOperationWithRequest 是实例方法,不是类方法:

AFHTTPRequestOperation *operation = [_httpClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
// do something
;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
;
}];

顺便说一句,你链接的教程是正确的。

关于iphone - map View 崩溃时的 GDirections 路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11825876/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com