【问题标题】:IOS: Drawing best route path using google mapIOS:使用谷歌地图绘制最佳路线路径
【发布时间】:2016-06-09 06:48:45
【问题描述】:

我已经将我的 IOS 应用程序与谷歌地图集成。我需要使用谷歌地图绘制从我当前位置到某个(XYZ)目的地的最佳路线路径(更短的距离)。而且我还需要那种交通工具(比如:开车、步行、交通工具)

有人可以帮我做这件事吗?任何框架在两点之间画线(最佳路径绘制)并在我点击驱动器时导航该路径(每 2 秒更新一次路径)

【问题讨论】:

标签: ios objective-c iphone


【解决方案1】:

第一步

向 Google 路线API 发送URL request。

第 2 步

当您收到JSON file 时,请完成所有步骤。

第 3 步

解码点对象。


首先获取您路线上的所有点坐标,然后在路径中添加这些点(这些点的纬度和经度),您可以根据该点绘制路径...试试这个:

GMSCameraPosition *position=[GMSCameraPosition cameraWithLatitude:15.5203 longitude:68.8567 zoom:10];
_mView =[GMSMapView mapWithFrame:CGRectZero camera:position];
_mView.myLocationEnabled=YES;
GMSMarker *marker=[[GMSMarker alloc]init];
marker.position=CLLocationCoordinate2DMake(15.5203, 68.8567);
marker.icon=[UIImage imageNamed:@"a.png"] ;
marker.groundAnchor=CGPointMake(0.5,0.5);
marker.map=_mView;
GMSMutablePath *path = [GMSMutablePath path];   
[path addCoordinate:CLLocationCoordinate2DMake(@(15.520).doubleValue,@(68.856).doubleValue)];
[path addCoordinate:CLLocationCoordinate2DMake(@(15.7).doubleValue,@(73.8567).doubleValue)];

GMSPolyline *rect = [GMSPolyline polylineWithPath:path];
rect.strokeWidth = 2.f;
rect.map = _mView;
self.view=_mView;

更新

    NSString *urlString = [NSString stringWithFormat:
                       @"%@?origin=%f,%f&destination=%f,%f&sensor=true&key=%@",
                       @"https://maps.googleapis.com/maps/api/directions/json",
                       mapView.myLocation.coordinate.latitude,
                       mapView.myLocation.coordinate.longitude,
                       destLatitude,
                       destLongitude,
                       @"Enter Google API String Here"];
NSURL *directionsURL = [NSURL URLWithString:urlString];


ASIHTTPRequest *req = [ASIHTTPRequest requestWithURL:directionsURL];
[req startSynchronous];
NSError *error = [req error];
if (!error) {
    NSString *response = [req responseString];
    NSLog(@"%@",response);
    NSDictionary *json =[NSJSONSerialization JSONObjectWithData:[req responseData] options:NSJSONReadingMutableContainers error:&error];
    GMSPath *path =[GMSPath pathFromEncodedPath:json[@"routes"][0][@"overview_polyline"][@"points"]];
    GMSPolyline *singleLine = [GMSPolyline polylineWithPath:path];
    singleLine.strokeWidth = 7;
    singleLine.strokeColor = [UIColor greenColor];
    singleLine.map = self.mapView;
}
else NSLog(@"%@",[req error]);

【讨论】:

  • 你能给我一个链接,请简要解释一下吗
  • 检查我更新的答案兄弟.... 也看到这个视频...talkminer.com/…
  • 感谢您的建议。但我已经这样做了。我需要像我们在谷歌地图中那样导航从我当前位置到目的地的路线(例如:10mtr 后左转等)。而且我需要每 2 秒刷新一次地图。我还需要知道什么逻辑谷歌地图必须绘制最佳路径。
  • 这似乎有点困难。我只是将我的源值和目标值重定向到本机地图。例如:UIApplication *ourApplication = [UIApplication sharedApplication]; NSString *ourPath = @"maps.apple.com/?saddr=Cupertino&daddr=San+Francisco"; NSURL *ourURL = [NSURL URLWithString:ourPath]; [ourApplication openURL:ourURL];现在我需要添加我的印度来源和目的地,但它不会在字符串之间使用 , 运算符。我无法在这里提供印度城市的位置。你能帮我吗
【解决方案2】:

希望对你有帮助

NSString *originString = [NSString stringWithFormat:@"%f,%f", origin.coordinate.latitude, origin.coordinate.longitude];
    NSString *destinationString = [NSString stringWithFormat:@"%f,%f", destination.coordinate.latitude, destination.coordinate.longitude];
    NSString *directionsAPI = @"https://maps.googleapis.com/maps/api/directions/json?";
    NSString *directionsUrlString = [NSString stringWithFormat:@"%@&origin=%@&destination=%@&mode=driving", directionsAPI, originString, destinationString];
    NSURL *directionsUrl = [NSURL URLWithString:directionsUrlString];


    NSURLSessionDataTask *fetchDirectionsTask = [[NSURLSession sharedSession] dataTaskWithURL:directionsUrl completionHandler:
                                                 ^(NSData *data, NSURLResponse *response, NSError *error)
                                                 {
                                                     NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
                                                     if(error)
                                                     {
                                                         if(completionHandler)
                                                             completionHandler(nil);
                                                         return;
                                                     }

                                                     NSArray *routesArray = [json objectForKey:@"routes"];

                                                     GMSPolyline *polyline = nil;
                                                     if ([routesArray count] > 0)
                                                     {
                                                         dispatch_async(dispatch_get_main_queue(), ^{
                                                         NSDictionary *routeDict = [routesArray objectAtIndex:0];
                                                         NSDictionary *routeOverviewPolyline = [routeDict objectForKey:@"overview_polyline"];
                                                         NSString *points = [routeOverviewPolyline objectForKey:@"points"];
                                                 GMSPolyline *polyline = [GMSPolyline polylineWithPath: [GMSPath pathFromEncodedPath: points]];
                                                         polyline.map = _mView;
                                                         polyline.strokeColor = [UIColor colorWithRed:0/255.0 green:4/255.0 blue:255/255.0 alpha:1.0];
                                                         polyline.strokeWidth = 4.0f;
                                                             });
                                                         };


                                                     if(completionHandler)
                                                         completionHandler(polyline);
                                                 }];
    [fetchDirectionsTask resume];
}

【讨论】:

  • 你能解释一下你的代码吗?只是在这里转储代码并不能帮助人们学习。
  • 它是一个directionsAPI,它提供您经过的纬度和经度之间的路线,并在GMSPolyline的帮助下绘制路线
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-29
相关资源
最近更新 更多