How to Draw turn by turn route in iphone/ipad native app?

1. First import below frameworks in viewcontroller.h file.

#import <CoreLocation/CoreLocation.h>

#import <MapKit/MapKit.h>

#import <AddressBook/AddressBook.h>

 

2. Now in viewcontroller.m file get the starting and destination address using below method. Repeat below methods twice to get both address values.

 

- (void) getAddressFromLatLon:(CLLocation *)bestLocation

{

        CLGeocoder *geocoder = [[CLGeocoder alloc] init] ;

        [geocoder reverseGeocodeLocation:bestLocation   completionHandler:^(NSArray *placemarks, NSError *error)

        {

                if (error){

                NSLog(@"Geocode failed with error: %@", error);

                return;

                }

        CLPlacemark *placemark = [placemarks objectAtIndex:0];

        street_start = [placemark.addressDictionary objectForKey:@"Street"];                                  // street_end for destination location

        city_start = [placemark.addressDictionary objectForKey:@"City"];                                          // city_end for destination location

        state_start = [placemark.addressDictionary objectForKey:@"State"];                                      // state_end for destination location

        pin_start = [placemark.addressDictionary objectForKey:@"ZIP"];                                           // pin_end for destination location

        country_start = [placemark.addressDictionary objectForKey:@"Country"];                            // country_end for destination location

        Address_start = [NSString stringWithFormat:@"%@ %@ %@ %@, %@",street,city,state,pin, country]; // Address_end for destination location

        }];

}

 

3. After getting two address values. Just call the below method twice for both address values.And get the coordinate values.

 

- (void)UserDirections

{

        CLGeocoder *geocoder = [[CLGeocoder alloc] init];

        NSString *addressString = [NSString stringWithFormat:@"%@",Address_start];    // Address_end for destination location

        ///here address value pass in different variable on 2nd time method call for destination address

 

        [geocoder geocodeAddressString:addressString  completionHandler:^(NSArray *placemarks, NSError *error) 
        {
                if (error)

                {

                        NSLog(@"Geocode failed with error: %@", error);

                        return;

                }

                if (placemarks && placemarks.count > 0)

                {

                        CLPlacemark *placemark = placemarks[0];

                        CLLocation *location = placemark.location;

                        _coords_start = location.coordinate;  //_coords_end for destination location

                         

                        //Here coordinate value store in different variable on 2nd time method call for destination address

                }

        }];

}

 

 

4. After Getting two coordinate values pls call below method to draw route in iphone/ipad native map.

 

-(void)showMap

{

        NSDictionary *address_start = @{

                                        (NSString *)kABPersonAddressStreetKey: street_start,

                                        (NSString *)kABPersonAddressCityKey: city_start,

                                        (NSString *)kABPersonAddressStateKey: state_start,

                                        (NSString *)kABPersonAddressZIPKey: pin_start

        };

        NSDictionary *address_end = @{

                                        (NSString *)kABPersonAddressStreetKey: street_end,

                                        (NSString *)kABPersonAddressCityKey: city_end,

                                        (NSString *)kABPersonAddressStateKey: state_end,

                                        (NSString *)kABPersonAddressZIPKey: pin_end

        };

        MKPlacemark *place_start = [[MKPlacemark alloc] initWithCoordinate:_coords_start addressDictionary:address_start];

        MKPlacemark *place_end = [[MKPlacemark alloc] initWithCoordinate:_coords_end addressDictionary:address_end];

        MKMapItem *mapItem_start = [[MKMapItem alloc]initWithPlacemark:place_start];

        MKMapItem *mapItem_end = [[MKMapItem alloc]initWithPlacemark:place_end];

 

        NSDictionary *options = @{

                MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving

        };

        NSArray *mapPoints = @[mapItem_start , mapItem_end];

        [MKMapItem openMapsWithItems:mapPoints launchOptions:options];

}