2016-09-17 110 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;

相關問題