我正在开发一个 iOS 应用程序,我必须在其中获取从一个位置到另一个位置的路线。我正在使用 MKDirectionsRequest API 获取路线。这个 api 给出了两个位置之间的方向。但问题是这个api返回的路线没有考虑车辆行驶的方向(路线)。有没有办法在iOS中获取用户当前位置到目的地之间的路线,这也考虑了移动车辆的路线。目前我正在使用以下功能:
-(void)displayDirections{
CLLocation *currentLocation = [[CLLocation alloc] initWithLatitude:currentLocationLatitude longitude:currentLocationLongitude];
MKPlacemark *placemarker0 = [[MKPlacemark alloc]initWithCoordinate:currentLocation.coordinate];
MKMapItem *myMapItem0 = [[MKMapItem alloc] initWithPlacemark:placemarker0];
CLLocation *location = [[CLLocation alloc] initWithLatitude:destinationLocationLatitude longitude:destinationLocationLongitude]
MKPlacemark *placemarker1 = [[MKPlacemark alloc]initWithCoordinate:location.coordinate];
MKMapItem *myMapItem1 = [[MKMapItem alloc] initWithPlacemark:placemarker1];
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
[request setSource:myMapItem0];
[request setDestination:myMapItem1];
[request setTransportType:MKDirectionsTransportTypeAny];
[request setRequestsAlternateRoutes:YES]; // Gives you several route options.
MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
__weak COMSiteDetailViewController *weakSelf = self;
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
if (!error) {
[weakSelf.mkMapView removeOverlays:weakSelf.mkMapView.overlays];
NSArray *routes = [response routes];
NSArray *sortedArray;
sortedArray = [routes sortedArrayUsingComparator:^NSComparisonResult(MKRoute *a, MKRoute *b) {
CLLocationDistance first = a.distance;
CLLocationDistance second = b.distance;
return first < second;
}];
int count = 1;
for (MKRoute *route in sortedArray) {
if(count < sortedArray.count){
route.polyline.title = @"longer";
}else{
route.polyline.title = @"shortest";
}
count++;
[self.mkMapView addOverlay:[route polyline] level:MKOverlayLevelAboveRoads];
}
}
}];
}
提前致谢!