iphone - 在 iOS 7 中使用谷歌地图绘制折线?

标签 iphone objective-c google-maps ios7

使用这个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 。 NSLog 对于查看您正在使用的内容很有用。

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

然后,您可以将路径转换为折线并将其绘制在 map View 上,并根据需要设置描边颜色和描边宽度。

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

然后将 polyline.map 属性设置为您的 map View 。

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 中使用谷歌地图绘制折线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20512578/

相关文章:

ios - Facebook 不会完全注销-ios

ios - 将用户个人资料照片动态映射到iOS中的图钉

javascript - 如何在没有 API key 的情况下使用谷歌地图

android - 如何在新层中使用phonegap打开外部链接?

iphone - 使用 NSUserDefaults 保存游戏设置/状态的优点?

iphone - 有哪些值得研究的优秀开源 iOS 项目?

objective-c - 具有多个时间间隔的 NSTimer

ios - 使用双向弱指针

ios - Storyboard 中的 View 在 iPhone 6 上无法正确调整大小

google-maps - Google Places API未返回place_id