如何在两个地点之间获得多条路线?

时间:2017-03-10 14:01:41

标签: ios objective-c google-maps google-maps-api-3 core-location

使用以下代码。我只获得一条路线。实际上它有三条路线,但无法用这段代码显示3条路线。我怎么能这样做?

NSString *baseUrl=[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=Hyderabad&destination=Sangareddy&key=%@&alternatives=true",key];

    NSURL *url = [NSURL URLWithString:[baseUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse
*response, NSData *data, NSError *connectionError) {
        if(!connectionError){
            NSDictionary *result        = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

            NSArray *routes=[result objectForKey:@"routes"];

            NSDictionary *routesdict=[routes objectAtIndex:0];

            NSArray *temproutearr=[[[routesdict objectForKey:@"legs"]objectAtIndex:0]objectForKey:@"steps"];
            _pointsarr=[[NSMutableArray alloc]init];
            for(int i=0;i<temproutearr.count;i++){
                [_pointsarr addObject:[[[temproutearr objectAtIndex:i]objectForKey:@"polyline"]objectForKey:@"points"]];
                    }
            GMSMutablePath *path = [GMSMutablePath path];

            for (NSString *polyStr in _pointsarr) {
                GMSPath *p = [GMSPath pathFromEncodedPath:polyStr];
                for (NSUInteger i=0; i < p.count; i++) {
                    [path addCoordinate:[p coordinateAtIndex:i]];
                }
            }
      polyPath.map= _mapView;
        }
    }];

2 个答案:

答案 0 :(得分:2)

试试这个:

NSString *baseUrl=[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=Hyderabad&destination=Sangareddy&key=%@&alternatives=true",key];

NSURL *url = [NSURL URLWithString:[baseUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse
*response, NSData *data, NSError *connectionError) {
    if(!connectionError){
        NSDictionary *result= [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

NSArray *routesArray = [result objectForKey:@"routes"];
GMSPolyline *polyline = nil;
for (NSDictionary *routeDict in routesArray) {
    NSDictionary *routeOverviewPolyline = [routeDict objectForKey:@"overview_polyline"];
    NSString *points = [routeOverviewPolyline objectForKey:@"points"];
    GMSPath *path = [GMSPath pathFromEncodedPath:points];
    polyline = [GMSPolyline polylineWithPath:path];
    [polyline setStrokeColor:[UIColor redColor]];
    polyline.map = _mapView;
}

答案 1 :(得分:1)

如果您在查询链接中添加了alternatives=true,则会获得3条路线。

NSArray *routes=[result objectForKey:@"routes"];//routes count will be3

然后,不要在NSDictionary *routesdict中存储第一个索引,而是尝试使用for loop绘制路径。

<强>夫特

for item in 0..<(jsonData.valueForKey("routes") as! [[String:AnyObject]]).count
{
   let pathStr = ((jsonData.valueForKey("routes") as! [[String:AnyObject]])[item]["overview_polyline"] as! [String:String])["points"]!
   let path = GMSPath(fromEncodedPath: pathStr)
   dispatch_async(dispatch_get_main_queue(), {
   let polyline = GMSPolyline(path: path)
   polyline.strokeColor = UIColor.blue
   polyline.strokeWidth = 5.0
   polyline.tappable = true
   polyline.map = self.mapView
   })
}