使用此link

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

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


谁能知道。请帮助我解决此问题。

最佳答案

您可以使用GoogleMapsDirectionGoogle Directions API获取路径或DirectionResponse。 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处的第一条路线)。

GMSPath *path = [GMSPath pathFromEncodedPath:routes[0][@"overview_polyline"][@"points"]];


然后,您可以将路径转换为折线,然后在mapView上绘制它,并根据需要设置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")
}];


我发现CocoaPods对于安装Google Maps SDK和GoogleMapsDirection非常有用。

关于iphone - 在iOS 7中使用Google Map绘制折线?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20512578/

10-11 17:14
查看更多