【问题标题】:Multiple Routes Between two Locations in MkmapviewMkmapview 中两个位置之间的多条路线
【发布时间】:2017-08-24 06:27:41
【问题描述】:
  • 如何在两点之间获得一条或多条可能的路线?如果有人 已经解决了这个问题可以告诉我吗?

【问题讨论】:

  • 这里面的RouteObject怎么样,有完整的文件或项目吗
  • Vikas Rajput 路线将创建两个点起点和终点。位置更新最后一个点,您可以制作上一个点并可以创建路线
  • 只看到这个link 但我问的是多条路线

标签: ios objective-c mkmapview mkdirection


【解决方案1】:

使用 MapKit

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

MKDirectionsRequest 声明中定义的那样

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.

完整代码

目标-C

 - (void)createRouteFrom:(CLLocationCoordinate2D)from to:(CLLocationCoordinate2D)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];
            }
        }];
    }
}

斯威夫特

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)
            }
        }
        
    }

希望对你有帮助

【讨论】:

    【解决方案2】:

    使用这个:-

     NSString *baseUrl=[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?   origin=indore&destination=bhopal&key=%@&alternatives=true”,APIKey];
    
    NSURL *url = [NSURL URLWithString:[baseUrl 
    stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    
     NSURLSessionConfiguration *defaultConfigObject = 
    [NSURLSessionConfiguration defaultSessionConfiguration];
     NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: 
    defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];
    
    
    NSURLSessionDataTask * dataTask = [defaultSession dataTaskWithURL:url
                                                         completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if(error == nil)
        {
           NSDictionary *result= [NSJSONSerialization JSONObjectWithData:data 
    options:0 error:nil];
    // this will return all possible roots
    NSArray *routes = [result objectForKey:@"routes"];
    //add The  polyline between locations.    
        }
    
    }];
    
    [dataTask resume];       
    

    【讨论】:

      猜你喜欢
      • 2012-10-01
      • 2016-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-19
      • 1970-01-01
      • 2012-03-06
      • 1970-01-01
      相关资源
      最近更新 更多