2
A
回答
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)
}
}
}
希望這有助於
1
使用本: -
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];
相關問題
- 1. 兩個位置之間的路線
- 2. 如何在iPhone中的兩個位置之間繪製路線?
- 3. 如何在CloudMade上的兩個位置之間繪製路線
- 4. 獲取兩個位置之間的路線方向
- 5. 谷歌地圖上兩個位置之間的Android路線
- 6. 獲取兩個位置之間的距離,如Google路線
- 7. 二維矩陣中兩個位置之間的最短路徑
- 8. jQuery:div中兩個位置之間有多少個元素
- 9. 谷歌地圖中的兩個位置與多個航點之間的最佳路線Android工作室
- 10. 兩個GEO位置之間的距離
- 11. android使用離線precatche地圖來標記兩個位置之間的道路?
- 12. 想畫任意兩個位置之間的直線的Android
- 13. 繪製2個位置之間的路線Google Maps API Android V2
- 14. 鏈路之間的兩個
- 15. 在兩個位置之間畫一條線 - WhirlyGlobe
- 16. 獲取問題在Android SDK中兩個位置之間的地圖繪製線
- 17. Python關係/兩個或多個線程之間的通信
- 18. iOS Swift - 在MKMapView中使用多個座標繪製路線
- 19. mapkit中多個點之間的最短路線應用程序
- 20. 計算兩個位置之間的方位
- 21. 兩個表之間的多個外鍵
- 22. 在iOS中的GMSMapView無限位置之間繪製路線
- 23. 谷歌地圖在我自己的應用程序中的兩個位置之間的行車路線
- 24. 在Xcode中的兩個座標之間繪製多段線
- 25. 如何計算兩個位置之間的時間差
- 26. 在jsp中調整兩個位置之間的距離
- 27. 計算Android中兩個位置點之間的速度
- 28. MKMapView在定製路線上獲得座標之間的距離
- 29. 如何在iOS中以文本的形式獲取兩個位置之間的路線
- 30. 兩個陣列之間的Perl迴路
檢查這個https://stackoverflow.com/questions/23529970/ios-mapkit-connect-multiple-points-on-map-over-roads/23818032#23818032 –
RouteObject在這個怎麼樣,你有一個完整的文件或項目 –
Vikas Rajput路由將創建兩個點的起點和終點。位置更新最後一點,你可以做出前一點,並可以創建路線 –