2012-08-07 115 views
0

我正在使用Google Directions iOS API。我使用JSON而不是XML来获取数据。但我正在使用AFNetworking来简化这一点。 github上有AFNetworking。现在,我可以在MKMapView上显示从一个位置到另一个位置的覆盖路线。这里是我的代码:iPhone - Google Directions API Response Problems

// AFNETWORKING ========================================================== 

     AFHTTPClient *_httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://maps.googleapis.com/"]]; 

    [_httpClient registerHTTPOperationClass: [AFJSONRequestOperation class]]; 

    [_httpClient setDefaultHeader:@"Accept" value:@"application/json"]; 

    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:@"false" forKey:@"sensor"]; 

    [parameters setObject:@"driving" forKey:@"mode"]; 

    [parameters setObject:@"metric" forKey: @"units"]; 

    NSMutableURLRequest *request = [_httpClient requestWithMethod:@"GET" path: @"maps/api/directions/json" parameters:parameters]; 

    request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData; 

    AFHTTPRequestOperation *operation = [_httpClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) { 

     NSInteger statusCode = operation.response.statusCode; 

     if (statusCode == 200) { 

      [self parseResponse:responseObject]; 

     } else { 

     } 

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { }]; 

    [_httpClient enqueueHTTPRequestOperation:operation]; 

// ROUTE SETUP AND RESPONSE RECIEVED METHOD ========================================================== 


- (void)parseResponse:(NSDictionary *)response { 

NSArray *routes = [response objectForKey:@"routes"]; 

NSDictionary *routePath = [routes lastObject]; 

if (routePath) { 

    NSString *overviewPolyline = [[routePath objectForKey: @"overview_polyline"] objectForKey:@"points"]; 

    NSLog(@"Status: %@", [response objectForKey: @"status"]); 

    NSLog(@"Legs: %@", [routePath objectForKey: @"legs[]"]); 

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

    polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps]; 
    [self.mapView addOverlay:polyLine]; 
} 

} 

这是获取路线功能的主要代码。它运作良好!但是现在我想要做的是获得方向列表并且完成持续时间和总结。所以我深入了解Google Directions API Documentation,它告诉我使用不同的字典,数组和对象。但我没有运气。我的状态是OK。当我记录routes数组的数量时,它只有一个对象。那么最重要的,legs[]阵列是NULL

NSLog(@"Legs is: %@", [routePath objectForKey: @"legs[]"]); 

输出:

腿是(空)

Legs[]包括一切像方向列表和持续时间很重要的。 Summary不是NULL,它给出了路线绕过的街道之一的名称。我不知道这是一个总结。可能是因为routes阵列中只有一个对象。 waypoint_orderwarnings[]也是NULLbounds和当然overview_polyline是有效的,这就是我如何得到的路线工作。

那么这里有什么问题? Google Directions API在我需要时提供NULL这么多对象的原因是什么?

谢谢!

回答

0

好的,问题在于对方向的响应时间太长。我预计legs[]响应立即与overview_polyline一起交付。所以我只能等待响应进入,这给了我一个插入缓冲区UI的机会。

相关问题