2016-09-17 88 views
0

试图在我的简单项目中使用谷歌方向api。 但工作返回我失败。 它有什么问题?从谷歌代码上githubGoogle Directions API

实例在文件MDDirectionService.m我们有方法“setDirectionsQuery” 但今天查询谷歌API有另一种结构。 更改查询中使用API​​密钥,但代码ViewController.m文件的方法中调用FUNC“didTapAtCoordinate”不工作

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]; 
    } 

- (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的边界' ***第一个抛出调用堆栈:

回答

0

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

这可能是由于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;

相关问题