1

I am working on one app, I need to show route directions between two coordinates. I have used MKDirections and have passed two coordinates as source and destination, however on mapView its not showing any route or drawing any polyline. Below is my code. In MKDirections its always showing nil. Please let me know what I am doing wrong.

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.activityIndicator.hidden = YES;
    self.routeDetailsButton.hidden = YES;
    self.routeDetailsButton.enabled = NO;
    self.mapView.delegate = self;
    self.mapView.showsUserLocation = YES;
    self.navigationItem.title = @"RouteMaster";

}

#pragma mark - MapKit delegate methods

- (void)mapView:(MKMapView *)aMapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    CLLocationCoordinate2D loc = [userLocation.location coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 1000.0f, 1000.0f);
    [self.mapView setRegion:region animated:YES];
}


- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (IBAction)handleRoutePressed:(id)sender {
    // We're working

    CLLocationCoordinate2D sourceCoords = CLLocationCoordinate2DMake(28.6100, 77.2300);
    MKPlacemark *sourcePlacemark = [[MKPlacemark alloc] initWithCoordinate:sourceCoords addressDictionary:nil];

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

    CLLocationCoordinate2D destinationCoords = CLLocationCoordinate2DMake(18.9750, 72.8258);
    MKPlacemark *destinationPlacemark = [[MKPlacemark alloc] initWithCoordinate:destinationCoords addressDictionary:nil];

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

    MKDirectionsRequest *request = [[MKDirectionsRequest alloc]init];
    [request setSource:srcMapItem];
    [request setDestination:distMapItem];

    MKDirections *direction = [[MKDirections alloc]initWithRequest:request];
    [direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {

         if (error)
             NSLog(@"Error %@", error.description);
        else
            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]);
            }];
        }];
    }];

}

#pragma mark - Utility Methods
- (void)plotRouteOnMap:(MKRoute *)route
{
    if(_routeOverlay) {
        [self.mapView removeOverlay:_routeOverlay];
    }

    // Update the ivar
    _routeOverlay = route.polyline;

    // Add it to the map
    [self.mapView addOverlay:_routeOverlay];

}


#pragma mark - MKMapViewDelegate methods
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
    renderer.strokeColor = [UIColor redColor];
    renderer.lineWidth = 4.0;
    return  renderer;
}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay {

    if ([overlay isKindOfClass:[MKPolyline class]]) {
        MKPolylineView* aView = [[MKPolylineView alloc]initWithPolyline:(MKPolyline*)overlay] ;
        aView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];
        aView.lineWidth = 10;
        return aView;
    }
    return nil;
}
Evgeny Karkan
  • 8,782
  • 2
  • 32
  • 38
Kashif Jilani
  • 1,207
  • 3
  • 22
  • 49
  • You don't need to specify an addressDictionary but be aware that MKDirections does not support directions in every part of the world. This code must be showing the error "MKErrorDomain Code=5 Directions Not Available". Test this code with coordinates in the US or Europe. –  Jun 23 '14 at 12:26

2 Answers2

1

Mostly this error occur if [_mapView setRegion:region] area and polyline route path are not on same region(both should need to display on same screen). In my case I was searching for USA route path while my region was India. So it wont call to -(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id)overlay; function.

garg
  • 2,651
  • 1
  • 24
  • 21
0

add this method

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

    MKPolylineRenderer  * routeLineRenderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];

    routeLineRenderer.strokeColor = [UIColor blueColor];
    routeLineRenderer.lineWidth = 4;
    return routeLineRenderer;
}
user2931321
  • 468
  • 1
  • 7
  • 28