如何在两个位置之间获得多条路线?

Posted

技术标签:

【中文标题】如何在两个位置之间获得多条路线?【英文标题】:How to get multiple routes between two locations? 【发布时间】:2017-03-10 14:01:41 【问题描述】:

使用以下代码。我只得到一条路线。实际上它有 3 条路线,但无法使用此代码显示 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;
        
    ];

【问题讨论】:

查看***.com/questions/6095435/… Find the number of routes between two places的可能重复 @jigar 没明白......它说要在查询链接中添加alternatives=true......但输出没有变化 【参考方案1】:

试试这个:

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;

【讨论】:

【参考方案2】:

如果您在查询链接中添加了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
   )

【讨论】:

然后我会得到每条路线的折线点,所以我应该将所有点添加到 GMSpath 然后到 GMSpolyline 对吗? 您将获得 3 个折线点。然后你应该创建 3 个 GMSPath 和 3 个 GMSPolylines。您可以设置不同的strokeColorstrokeWidth 来突出显示特定的折线。

以上是关于如何在两个位置之间获得多条路线?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Swift 中使用 MapKit 在两个位置之间绘制路线?

如何在android中显示两个位置之间的路线?

如何在苹果地图中的两个位置之间绘制路线。通过 openURL

如何获取地图上两点之间的路线方向以绘制行车路线?

如何在iOS中以文本形式获取两个位置之间的路线

如何在从 ListView 项获取的两个地理点之间绘制标记和路线?