在iOS 7中使用谷歌地图绘制折线? [英] Draw polyline using google map in iOS 7?

查看:180
本文介绍了在iOS 7中使用谷歌地图绘制折线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此链接



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

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

任何人都可以知道,请帮我解决这个问题。

解决方案

您可以使用 GoogleMapsDirection 获取路径或DirectionResponse来自 Google路线API



$ p $ [[GMDirectionService sharedInstance] getDirectionsFrom:origin:目标成功:^ (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]);

$ b}失败:^(NSError * error){
NSLog(@无法到达服务器)
}];

一旦你有了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; 

最后放在一起

<$ p $ ([directionResponse statusOK]){
NSLog(@持续时间: %@,[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;

$ b}失败:^(NSError * error){
NSLog(@无法到达服务器)
}];

我发现 CocoaPods ,可用于安装Google Maps SDK和GoogleMapsDirection。


Using this link .

i tried to get route direction from one coordinates to another coordinates but didn't get route. Below mention used coding...

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

Can anyone know.Help me to solve this issue..

解决方案

You can use GoogleMapsDirection to get the path or DirectionResponse from the Google Directions API. The NSLogs are useful to see what you are working with.

[[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")
}];

Once you have the json you can get the path points (this shows the first route at index 0).

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

Then you can convert the path to a polyline and draw it on your mapView and set the strokeColor and strokeWidth if desired.

GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.strokeColor = [UIColor redColor];
polyline.strokeWidth = 5.f;

Then set the polyline.map property to your mapView.

polyline.map = mapView;

Finally put it all together

[[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")
}];

I find CocoaPods useful for installing the Google Maps SDK and GoogleMapsDirection.

这篇关于在iOS 7中使用谷歌地图绘制折线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆