使用这个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_;
谁知道。帮我解决这个问题..
最佳答案
您可以使用GoogleMapsDirection从 Google 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/