【问题标题】:Draw polyline using google map in iOS 7?在 iOS 7 中使用谷歌地图绘制折线?
【发布时间】:2013-12-11 07:00:08
【问题描述】:

使用这个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_;

谁能知道。帮我解决这个问题..

【问题讨论】:

    标签: iphone objective-c google-maps ios7


    【解决方案1】:

    您可以使用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 很有用。

    【讨论】:

    • 嗨...它总是失败的请求。你能告诉我应该是什么结果..我用你的代码来获取方向和折线
    • 它需要“AFNetworking”,AFNetworking 的当前版本是 2.3.1,并且它没有需要“GMHTTPClient”作为基类的 AFHttpClient 类。如果我们使用旧版本的“AFNetworking”,那么它可以正常工作
    猜你喜欢
    • 2018-03-06
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多