Get directions from one location to another based on vehicle course

68 views Asked by At

I am developing an iOS app where I have to get directions from one location to another. I am getting the directions using the MKDirectionsRequest API. This api gives direction between two locations. But the problem is that the route returned by this api do not consider the direction (course) in which the vehicle is moving. Is there any way to get route between user current location to the destination in iOS which also takes the course of the moving vehicle into consideration. Currently I am using the following function:

-(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]; 
            }
        }
    }];
}

Thanks in advance!

1

There are 1 answers

0
Ravi Kumar On

After searching the web I have found that neither Google API or Apple MapKit Directions API consider the heading or movement of the vehicle while giving the route information. It just consider the coordinates of the start/end locations and give the shortest route(s).

So there is no way, at the moment, to display route considering vehicle heading using Apple/Google Apis.

Although, there are other paid Apis such as https://developer.here.com which I used in my app. This api is very reliable and the pricing too is very reasonable.

Thanks!