Today We will learn how to draw path between two places without using GoogleAPI and just using the methods of MKMapView.

Steps:

  1. Create a new xcode Project Lets say (MapKitTutorialDemo).
  2.  Now add MapKit Framework in the Project.

Screen Shot 2016-08-14 at 8.54.50 PM

  1. Now go to storyBoard. Take mapview and two label objects from object Library.

Screen Shot 2016-08-15 at 1.18.50 AM

  1. Now make proper IBOutlet Connection and set the delegate.

#import “ViewController.h”

#import <MapKit/MapKit.h>

@interface ViewController ()

@property (weak, nonatomic) IBOutlet MKMapView *mapView;

@property (weak, nonatomic) IBOutlet UILabel *distanceLabel;

5. Now go to Your ViewController.m. Here I am taking two fixed places as LosAngles and California .And I am also setting the Map region.  You can change it according to your own need.

@implementation ViewController{

    double distance;

}

– (void)viewDidLoad {

    [super viewDidLoad];

    distance = 0;

    NSArray *name=[[NSArray alloc]initWithObjects:

                   @”Los Angles”,

                   @”Sanfransisco”,nil];

    

    NSMutableArray *arrCoordinateStr = [[NSMutableArray alloc] initWithCapacity:name.count];

    

    [arrCoordinateStr addObject:@”34.0207504,-118.69193″];

    [arrCoordinateStr addObject:@”37.757815,-122.5076406″];

    

    for(int i = 0; i < name.count; i++)

    {

        [self addPinWithTitle:name[i] AndCoordinate:arrCoordinateStr[i]];

    }

    CLLocationCoordinate2D losAngles = CLLocationCoordinate2DMake(34.0207504,-118.69193);

    

    [self.mapView setRegion: MKCoordinateRegionMakeWithDistance(losAngles, 2955000, 1805000)];

    

    [self.mapView setMapType: MKMapTypeStandard];

    // Do any additional setup after loading the view, typically from a nib.

}

6. Now write down these method for drawing route and placing Annotation.

#pragma mark – AddPin Method

-(void)addPinWithTitle:(NSString *)title AndCoordinate:(NSString *)strCoordinate

{

    MKPointAnnotation *mapPin = [[MKPointAnnotation alloc] init];

    strCoordinate = [strCoordinate stringByReplacingOccurrencesOfString:@” “ withString:@””];

    NSArray *components = [strCoordinate componentsSeparatedByString:@”,”];

    double latitude = [components[0] doubleValue];

    double longitude = [components[1] doubleValue];

    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);

    mapPin.title = title;

    mapPin.coordinate = coordinate;

    

    [self.mapView addAnnotation:mapPin];

    [self loadRoute];

}

#pragma mark – Show Route Direction

-(void)loadRoute

{

    MKPlacemark *source = [[MKPlacemark alloc]initWithCoordinate:CLLocationCoordinate2DMake(34.0207504,-118.69193) addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@””,@””, nil] ];

    

    MKMapItem *srcMapItem = [[MKMapItem alloc]initWithPlacemark:source];

    [srcMapItem setName:@””];

    

    MKPlacemark *destination = [[MKPlacemark alloc]initWithCoordinate:CLLocationCoordinate2DMake(37.757815,-122.5076406) addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@””,@””, nil] ];

    

    MKMapItem *distMapItem = [[MKMapItem alloc]initWithPlacemark:destination];

    [distMapItem setName:@””];

    

    MKDirectionsRequest *request = [[MKDirectionsRequest alloc]init];

    [request setSource:srcMapItem];

    [request setDestination:distMapItem];

    [request setTransportType:MKDirectionsTransportTypeAutomobile];

    

    MKDirections *direction = [[MKDirections alloc]initWithRequest:request];

    

    [direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {

        

        NSLog(@”response = %@”,response);

        NSArray *arrRoutes = [response routes];

        [arrRoutes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

            

            MKRoute *rout = obj;

            

            MKPolyline *line = [rout polyline];

            [self.mapView addOverlay:line];

            NSLog(@”Rout Name : %@”,rout.name);

            NSLog(@”Total Distance (in Meters) :%f”,rout.distance);

            

            NSArray *steps = [rout steps];

            

            NSLog(@”Total Steps : %lu”,(unsigned long)[steps count]);

            

            [steps enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

                NSLog(@”Rout Instruction : %@”,[obj instructions]);

                NSLog(@”Rout Distance : %f”,[obj distance]);

                distance = distance +[obj distance];

                self.distanceLabel.text = [NSString stringWithFormat:@”The Total distance is %f”,distance];

            }];

        }];

    }];

    

}

– (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay

{

    

    if ([overlay isKindOfClass:[MKPolyline class]]) {

        MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];

        [renderer setStrokeColor:[UIColor blueColor]];

        [renderer setLineWidth:3.0];

        return renderer;

    }

    return nil;

}

@end

7.  Now run the code and see the desired output.

Screen Shot 2016-08-15 at 1.32.53 AM.png

Please do share your reviews and comment.

You can download the whole project source code from here.

Enjoy Coding  🙂

Advertisements