试图在我的简单项目中使用谷歌方向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的边界' ***第一个抛出调用堆栈: