我需要在两个位置之间显示多个路径,这两个位置是用户输入位置,并且还显示它们之间的最短路径。最短的路线是多条路线之一。
请帮助。
由于
答案 0 :(得分:2)
MKDirections calculateDirectionsWithCompletionHandler方法,返回一个MKDirectionsResponse对象,该对象包含可能的MKRoutes数组作为属性。
以下代码获取两个位置之间的所有路径,然后使用NSSortDescriptor对数组进行排序,以获得按其最短距离排序的路径数组。
希望它有所帮助!
//Create MKDirectionsRequest
MKDirectionsRequest * request = [[MKDirectionsRequest alloc] init];
request.source = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:sourceCoordinate addressDictionary:nil]];
request.destination = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:destinationCoordinate addressDictionary:nil]];
request.transportType = MKDirectionsTransportTypeAutomobile; //Or any other transport type.
//Get directions between two points
MKDirections * directions = [[MKDirections alloc] initWithRequest:request];
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"distance"
ascending:YES];
//Get the routes array sorted by distance
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedRoutes;
sortedRoutes = [response.routes sortedArrayUsingDescriptors:sortDescriptors];
//The fastest route is the one at index 0 of the sorted array
MKRoute * fastestRoute = [sortedRoutes objectAtIndex:0];
}];