jey*_*ran 2 iphone google-maps objective-c ios7
使用此链接.
我试图从一个坐标到另一个坐标获得路线方向,但没有得到路线.下面提到用过的编码......
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_;
Run Code Online (Sandbox Code Playgroud)
谁能知道.帮我解决这个问题..
Har*_*ala 10
您可以使用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")
}];
Run Code Online (Sandbox Code Playgroud)
一旦你有了json,你就可以获得路径点(这显示了索引0处的第一条路径).
GMSPath *path = [GMSPath pathFromEncodedPath:routes[0][@"overview_polyline"][@"points"]];
Run Code Online (Sandbox Code Playgroud)
然后,您可以将路径转换为折线并在mapView上绘制它,并根据需要设置strokeColor和strokeWidth.
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.strokeColor = [UIColor redColor];
polyline.strokeWidth = 5.f;
Run Code Online (Sandbox Code Playgroud)
然后将polyline.map属性设置为mapView.
polyline.map = mapView;
Run Code Online (Sandbox Code Playgroud)
最后把它们放在一起
[[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")
}];
Run Code Online (Sandbox Code Playgroud)
我发现CocoaPods对于安装Google Maps SDK和GoogleMapsDirection非常有用.
| 归档时间: |
|
| 查看次数: |
7433 次 |
| 最近记录: |