尝试在我的简单项目中使用谷歌方向api。但工作回报我失败了。它怎么了?github 上来自谷歌的代码示例
在文件MDDirectionService.m中,我们有方法"setDirectionsQuery"但如今查询googleapi有了另一种结构。我将查询更改为使用API KEY,但代码不工作
static NSString *kMDDirectionsURL = @"https://maps.googleapis.com/maps/api/directions/json?";
- (void)setDirectionsQuery:(NSDictionary *)query
withSelector:(SEL)selector
withDelegate:(id)delegate
{
NSArray *waypoints = query[@"waypoints"];
NSString *origin = waypoints[0];
int waypointCount = [waypoints count];
int destinationPos = waypointCount - 1;
NSString *destination = waypoints[destinationPos];
NSString *key = @"my key";
NSMutableString *url =
[NSMutableString stringWithFormat:@"%@&origin=%@&destination=%@&key=%@",
kMDDirectionsURL, origin, destination, key];
if(waypointCount > 2) {
[url appendString:@"&waypoints=optimize:true"];
int wpCount = waypointCount-2;
for(int i=1;i<wpCount;i++){
[url appendString: @"|"];
[url appendString:[waypoints objectAtIndex:i]];
}
}
url = [[url
stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding] mutableCopy];
_directionsURL = [NSURL URLWithString:url];
[self retrieveDirections:selector
withDelegate:delegate];
}
在ViewController.m文件中,方法调用func"didTapAtCoordinate">
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:
(CLLocationCoordinate2D)coordinate {
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(
coordinate.latitude,
coordinate.longitude);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.map = mapView_;
[waypoints_ addObject:marker];
NSString *positionString = [[NSString alloc] initWithFormat:@"%f,%f",
coordinate.latitude,coordinate.longitude];
[waypointStrings_ addObject:positionString];
if([waypoints_ count]>1){
NSString *sensor = @"false";
NSArray *parameters = [NSArray arrayWithObjects:sensor, waypointStrings_,
nil];
NSArray *keys = [NSArray arrayWithObjects:@"sensor", @"waypoints", nil];
NSDictionary *query = [NSDictionary dictionaryWithObjects:parameters
forKeys:keys];
MDDirectionService *mds=[[MDDirectionService alloc] init];
SEL selector = @selector(addDirections:);
[mds setDirectionsQuery:query
withSelector:selector
withDelegate:self];
}
}
我需要如何更改此代码,以便正确工作
NSDictionary *query = @{ @"sensor" : @"false", @"waypoints" : self.waypointStrings };
DirectionService *mds = [[DirectionService alloc] init];
SEL selector = @selector(addDirections:);
[mds setDirectionsQuery:query
withSelector:selector
withDelegate:self];
当我尝试使用它时,它在这个代码中有错误:
-(void)addDirections:(NSDictionary *)json
{
NSDictionary *routes = [json objectForKey:@"routes"][0];
NSDictionary *route = [routes objectForKey:@"overview_polyline"];
NSString *overview_route = [route objectForKey:@"points"];
GMSPath *path = [GMSPath pathFromEncodedPath:overview_route];
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.map = self.mapView;
}
消息错误如:
你拨打了-73.254689,40.840717
你拨打了-73.253745,40.837211
*由于未捕获的异常"NSRangeException"而终止应用程序,原因:"*-[__NSArray0 objectAtIndex:]:索引0超出了空NSArray的界限"***第一次抛出调用堆栈:
基于此SO线程,您将得到错误NSRangeException,因为您试图访问空的NSArray的array[0]。
这可能是由"一对一"错误引起的。
这个线程可能也有帮助。
该代码假设
response
数组始终至少具有一个元素。当数组为空时,它会中断,因此需要添加检查代码:NSArray *response = [responseObject objectForKey:@"results"]; if (response.count == 0) { // Continue loading more data: [self loadLatLong]; return; } NSDictionary *geo = [response[0] objectForKey:@"geometry"];
它崩溃是因为
response
没有任何对象。您应该在调用response[0]
之前进行检查。像这样的if ([response count] == 0) return;
。