• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - Mkmapview 中两个位置之间的多条路线

[复制链接]
菜鸟教程小白 发表于 2022-12-13 02:33:35 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

  • How to get one or more possible routes between two points? If anybody has worked on this can you please tell me?

Want like this



Best Answer-推荐答案


使用 MapKit

你需要让你的 [directionRequest setRequestsAlternateRoutes:YES]然后在 response.routes 你会得到可用的路线

As is defined in MKDirectionsRequest declaration

open var requestsAlternateRoutes: Bool // if YES and there is more than one reasonable way to route from source to destination, allow the route server to return multiple routes. Default is NO.

完整代码

objective-C

 - (void)createRouteFromCLLocationCoordinate2D)from toCLLocationCoordinate2D)destination{
    if(CLLocationCoordinate2DIsValid(from) && CLLocationCoordinate2DIsValid(destination))
    {
        MKDirectionsRequest * directionRequest = [[MKDirectionsRequest alloc]init];
        [directionRequest setSource:[[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:from]]];
        [directionRequest setSource:[[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:destination]]];
        [directionRequest setTransportType:MKDirectionsTransportTypeAutomobile];
        [directionRequest setRequestsAlternateRoutes:YES];
        
        MKDirections * directions = [[MKDirections alloc] initWithRequest:directionRequest];
        
        [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * _Nullable response, NSError * _Nullable error) {
            if(error == nil)
            {
                for (MKRoute * rute in response.routes) {
                    //DO WHAT YOU NEED WITH ROUTE
                }
                
                //here I add the first route
                MKRoute * firstOne = response.routes.firstObject;
                [self.map addOverlay:firstOne.polyline level:MKOverlayLevelAboveRoads];
                
                MKMapRect rect = [firstOne.polyline boundingMapRect];
                [self.map setRegion:MKCoordinateRegionForMapRect(rect) animated:YES];
            }
        }];
    }
}

swift

func createRouteTo(from: CLLocationCoordinate2D,to: CLLocationCoordinate2D) {
        
        if(CLLocationCoordinate2DIsValid(from) && CLLocationCoordinate2DIsValid(to))
        {
            self.showLoading(message: "Creando Ruta...")
            let directionRequest = MKDirectionsRequest()
            directionRequest.source = MKMapItem(placemark: MKPlacemark(coordinate: from, addressDictionary: nil))
            directionRequest.destination = MKMapItem(placemark: MKPlacemark(coordinate: to, addressDictionary: nil))
            directionRequest.transportType = .automobile
            //THIS MAKE the request for multiple routes if possible
            directionRequest.requestsAlternateRoutes = true
            
            // Calculate the direction
            let directions = MKDirections(request: directionRequest)
            
            directions.calculate { [unowned self] (directionResponse, error) in
                self.hideLoading(withDelay: 0.1)
                guard let response = directionResponse else {
                    if let error = error {
                        print("Error: \(error)")
                        let alert = UIAlertController.init(title:"Error Creando Ruta",
                                                           message:error.localizedDescription, preferredStyle: .alert)
                        alert.addAction(UIAlertAction.init(title: "Aceptar", style: .destructive, handler:nil))
                        self.present(alert, animated:true , completion:nil)
                    }
                    
                    return
                }
                
                if(self.currentRouteOverlay != nil)
                {
                    self.mapView.remove(self.currentRouteOverlay!)
                }
                
                
                //in response.routes you will get the routes avaiables
                for route in response.routes
                {
                    //Do what you need with the routes
                    
                } 

                //here I add the first to the MapView
                let route = response.routes[0]
                self.currentRouteOverlay = route.polyline
                self.mapView.add(route.polyline, level: .aboveRoads)
                
                let rect = route.polyline.boundingMapRect
                self.mapView.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)
            }
        }
        
    }

希望对你有帮助

关于ios - Mkmapview 中两个位置之间的多条路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45854405/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap