Display Route between 2 points on map with MapKit API

It was not possible to determine the route between two points before iOS7. But iOS7 has come with enhanced feature to allow the user to determine the route between two points.

iOS7 has come with the classes MKDirection, MKDirectionsRequest,  MKDirectionsResponse to determine the route between two points.

Let's understand how these classes work to accomplish this task with below given code.

CLLocationCoordinate2DsourceCoords = CLLocationCoordinate2DMake(37.7833, -122.4167);//san fransisco
MKPlacemark *sourcePlacemark = [[MKPlacemarkalloc] initWithCoordinate:sourceCoordsaddressDictionary:nil];
MKMapItem *source = [[MKMapItemalloc] initWithPlacemark:sourcePlacemark];

// Make the destination location
CLLocationCoordinate2DdestinationCoords = CLLocationCoordinate2DMake(34.0500, -118.2500);//los angeles
MKPlacemark *destinationPlacemark = [[MKPlacemarkalloc] initWithCoordinate:destinationCoordsaddressDictionary:nil];
MKMapItem *destination = [[MKMapItemalloc] initWithPlacemark:destinationPlacemark];
MKDirectionsRequest *directionsRequest = [MKDirectionsRequestnew];
    [directionsRequestsetSource:source];
    [directionsRequestsetDestination:destination];

MKDirections *directions = [[MKDirectionsalloc] initWithRequest:directionsRequest];
    [directionscalculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
if (error) {
return;
        }
// So there wasn't an error - let's plot those routes
_currentRoute = [response.routesfirstObject];
        [mapViewsetVisibleMapRect:_currentRoute.polyline.boundingMapRectanimated:NO];
        [mapViewaddOverlay:_currentRoute.polyline];
    }];

Now, let's understand this code step-by-step.

Step-1:

Create an object ofMKMapItem class for source location. This object is later used to request for direction. MKMapItem class contains information about a specific point on a map.

CLLocationCoordinate2DsourceCoords = CLLocationCoordinate2DMake(37.7833, -122.4167);//san fransisco
MKPlacemark *sourcePlacemark = [[MKPlacemarkalloc] initWithCoordinate:sourceCoordsaddressDictionary:nil];
MKMapItem *source = [[MKMapItemalloc] initWithPlacemark:sourcePlacemark];

Step-2:

Create another object of MKMapItem class same for destination location same as step-1.

CLLocationCoordinate2DdestinationCoords = CLLocationCoordinate2DMake(34.0500, -118.2500);//los angeles
MKPlacemark *destinationPlacemark = [[MKPlacemarkalloc] initWithCoordinate:destinationCoordsaddressDictionary:nil];
MKMapItem *destination = [[MKMapItemalloc] initWithPlacemark:destinationPlacemark];

Step-3:

Create an object of MKDirectionsRequest which will be used to request for route information later on. Set its source and destination points in between which you want to get the route.

MKDirectionsRequest *directionsRequest = [MKDirectionsRequestnew];
    [directionsRequestsetSource:source];
    [directionsRequestsetDestination:destination];

Step-4:

Create an object of MKDirections using the object of MKDirectionsRequest created in the previous step.

MKDirections *directions = [[MKDirectionsalloc] initWithRequest:directionsRequest];

Step-5:

Now you can get the route information using calculateDirectionsWithCompletionHandler: method provided by MKDirections class.

Step-6:

You will get the route information from the Apple server as the object of MKDirectionsResponse class. Using this object you can draw an overlay on the map.

MKDirections *directions = [[MKDirectionsalloc] initWithRequest:directionsRequest];
    [directionscalculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
if (error) {
return;
        }
// So there wasn't an error - let's plot those routes
_currentRoute = [response.routesfirstObject];
        [mapViewsetVisibleMapRect:_currentRoute.polyline.boundingMapRectanimated:NO];
        [mapViewaddOverlay:_currentRoute.polyline];
    }];

Step-7:

To draw an overlay on the map, you need to implement the delegate method of MKMapView as below:

- (MKOverlayRenderer *)mapView:(MKMapView *)mapViewrendererForOverlay:(id)overlay {
if ([overlay isKindOfClass:[MKPolylineclass]]) {
MKPolylineRenderer *renderer = [[MKPolylineRendereralloc] initWithOverlay:overlay];
        [renderersetStrokeColor:[UIColorredColor]];
        [renderersetLineWidth:4.0];
return renderer;
    }
returnnil;
}

 

Let's Think together, Say Something !