Iphone 在iOS 7中使用谷歌地图绘制多段线?

Iphone 在iOS 7中使用谷歌地图绘制多段线?,iphone,objective-c,google-maps,ios7,Iphone,Objective C,Google Maps,Ios7,用这个 我试图得到从一个坐标到另一个坐标的路线方向,但没有得到路线。下面提到使用的编码 NSArray *routesArray = [res objectForKey:@"routes"]; NSDictionary *routeDict = [routesArray objectAtIndex:0]; NSDictionary *routeOverviewPolyline = [routeDict objectForKey:@"overvie

用这个

我试图得到从一个坐标到另一个坐标的路线方向,但没有得到路线。下面提到使用的编码

        NSArray *routesArray = [res objectForKey:@"routes"]; 
        NSDictionary *routeDict = [routesArray objectAtIndex:0];
        NSDictionary *routeOverviewPolyline = [routeDict objectForKey:@"overview_polyline"];
        NSString *points = [routeOverviewPolyline objectForKey:@"points"];
        GMSPath *path = [GMSPath pathFromEncodedPath:points];

        polyline = [GMSPolyline polylineWithPath:path];
        polyline.strokeColor = [UIColor greenColor];
        polyline.strokeWidth = 10.f;

        polyline.map = mapView_;
任何人都可以知道。请帮助我解决此问题。

您可以使用从获取路径或方向响应。NSLogs有助于查看您正在使用的内容

[[GMDirectionService sharedInstance] getDirectionsFrom:origin to:destination        succeeded:^(GMDirection *directionResponse) {   
    if ([directionResponse statusOK]){
        NSLog(@"Duration : %@", [directionResponse durationHumanized]);
        NSLog(@"Distance : %@", [directionResponse distanceHumanized]);
        NSArray *routes = [[directionResponse directionResponse] objectForKey:@"routes"];
        // NSLog(@"Route : %@", [[directionResponse directionResponse] objectForKey:@"routes"]);

    }
} failed:^(NSError *error) {
        NSLog(@"Can't reach the server")
}];
一旦有了json,就可以获得路径点(这显示了索引0处的第一条路由)

然后,可以将路径转换为多段线,并在地图视图上绘制它,如果需要,还可以设置strokeColor和strokeWidth

GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.strokeColor = [UIColor redColor];
polyline.strokeWidth = 5.f;
然后将polyline.map属性设置为mapView

polyline.map = mapView;
最后把它们都放在一起

[[GMDirectionService sharedInstance] getDirectionsFrom:origin to:destination          succeeded:^(GMDirection *directionResponse) {   
    if ([directionResponse statusOK]){
        NSLog(@"Duration : %@", [directionResponse durationHumanized]);
        NSLog(@"Distance : %@", [directionResponse distanceHumanized]);
        NSArray *routes = [[directionResponse directionResponse] objectForKey:@"routes"];
        // NSLog(@"Route : %@", [[directionResponse directionResponse] objectForKey:@"routes"]);

        GMSPath *path = [GMSPath pathFromEncodedPath:routes[0][@"overview_polyline"]  [@"points"]];
        GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
        polyline.strokeColor = [UIColor redColor];
        polyline.strokeWidth = 5.f;
        polyline.map = mapView;

    }
} failed:^(NSError *error) {
        NSLog(@"Can't reach the server")
}];

我发现安装Google Maps SDK和GoogleMapsDirection很有用。

hi。。。请求总是失败的,你能告诉我结果是什么吗。。我使用了你的代码来获取方向和多段线。它需要“AFNetworking”,AFNetworking的当前版本是2.3.1,它没有AFHttpClient类,而AFHttpClient类需要“GMHTTPClient”作为基类。如果我们使用旧版本的“AFNetworking”,那么它的工作就可以了
[[GMDirectionService sharedInstance] getDirectionsFrom:origin to:destination          succeeded:^(GMDirection *directionResponse) {   
    if ([directionResponse statusOK]){
        NSLog(@"Duration : %@", [directionResponse durationHumanized]);
        NSLog(@"Distance : %@", [directionResponse distanceHumanized]);
        NSArray *routes = [[directionResponse directionResponse] objectForKey:@"routes"];
        // NSLog(@"Route : %@", [[directionResponse directionResponse] objectForKey:@"routes"]);

        GMSPath *path = [GMSPath pathFromEncodedPath:routes[0][@"overview_polyline"]  [@"points"]];
        GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
        polyline.strokeColor = [UIColor redColor];
        polyline.strokeWidth = 5.f;
        polyline.map = mapView;

    }
} failed:^(NSError *error) {
        NSLog(@"Can't reach the server")
}];