Google Directions API

时间:2016-09-17 17:45:53

标签: ios objective-c google-maps google-api google-direction

尝试在我的简单项目中使用google指令api。 但工作让我失败了。 它出什么问题了? 谷歌在github

上的代码示例

在文件MDDirectionService.m中,我们有方法“setDirectionsQuery” 但今天查询谷歌api有另一种结构。 我更改查询以使用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的界限' ***首先抛出调用堆栈:

1 个答案:

答案 0 :(得分:0)

基于此SO thread,您收到错误 NSRangeException ,因为您正在尝试访问数组为空的NSArray的数组[0]。

这可能是由off by one error引起的。

thread也可能有所帮助。

  

代码假设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;