ios - Mkmapview 中两个位置之间的多条路线
<p><ul>
<li> <blockquote>
<p>How to get one or more possible routes between two points? If anybody
has worked on this can you please tell me?</p>
</blockquote> </li>
</ul>
<p> <a href="/image/JHHER.png" rel="noreferrer noopener nofollow"><img src="/image/JHHER.png" alt="Want like this"/></a> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><h1>使用 MapKit</h1>
<p>你需要让你的 <code></code>然后在 <code>response.routes</code> 你会得到可用的路线</p>
<blockquote>
<p>As is defined in <code>MKDirectionsRequest</code> declaration</p>
<p><code>open var requestsAlternateRoutes: Bool // if YES and there is more</code>
<code>than one reasonable way to route from source to destination, allow the</code>
<code>route server to return multiple routes. Default is NO.</code></p>
</blockquote>
<h1>完整代码</h1>
<h1> objective-C </h1>
<pre><code> - (void)createRouteFrom:(CLLocationCoordinate2D)from to:(CLLocationCoordinate2D)destination{
if(CLLocationCoordinate2DIsValid(from) && CLLocationCoordinate2DIsValid(destination))
{
MKDirectionsRequest * directionRequest = [init];
initWithPlacemark:[ initWithCoordinate:from]]];
initWithPlacemark:[ initWithCoordinate:destination]]];
;
;
MKDirections * directions = [ 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;
;
MKMapRect rect = ;
;
}
}];
}
}
</code></pre>
<hr/>
<h1> swift </h1>
<pre><code>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 { (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
self.currentRouteOverlay = route.polyline
self.mapView.add(route.polyline, level: .aboveRoads)
let rect = route.polyline.boundingMapRect
self.mapView.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)
}
}
}
</code></pre>
<p>希望对你有帮助</p></p>
<p style="font-size: 20px;">关于ios - Mkmapview 中两个位置之间的多条路线,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/45854405/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/45854405/
</a>
</p>
页:
[1]