0

I am using display route on iOS 7 maps: addOverlay has no effect this to embed a map in my app. I am getting following errors: mkdirectionserrorcode = 6; mkerrorgeoerror="-403"; nslocalized description="Directions not available"; NSlocalizedfailureReason="A route to the nearest road cannot be determined";

I have set my source coordinates to apple headquarter and destination coordinates to stanford universtiy. Both are in usa california and still i get errors.

viewController.h file
--------------------------------

    #import <UIKit/UIKit.h>
    #import <MapKit/MapKit.h>
    #import <CoreLocation/CoreLocation.h>

    @interface ViewController : UIViewController <MKMapViewDelegate>
    @property (weak, nonatomic) IBOutlet MKMapView *myMapView;
    - (IBAction)button:(id)sender;

    @end

----------------------------------

viewController.m file
-----------------------------

    #import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        self.myMapView.delegate=self;
        [self.myMapView setShowsUserLocation:YES];
        NSLog(@" VIEW DID LOAD FINISHED");

        //------------------
        NSLog(@" DID UPDATE USER LOCATION STARTED EXECUTING");
        MKDirectionsRequest *request=[[MKDirectionsRequest alloc]init];
        //request.source=[MKMapItem mapItemForCurrentLocation];

        //setting source
        CLLocationCoordinate2D 
        sourceCoordinate=CLLocationCoordinate2DMake(37.33181,122.03118);
        MKPlacemark *sourcePlacemark=[[MKPlacemark 
        alloc]initWithCoordinate:sourceCoordinate addressDictionary:nil];
        MKMapItem *source=[[MKMapItem alloc]initWithPlacemark:sourcePlacemark];
        request.source=source;

        //setting destination
         CLLocationCoordinate2D destinationCoordinate=
         CLLocationCoordinate2DMake(37.33182,122.03118);
        MKPlacemark *destinationPlacemark=
        [[MKPlacemark alloc]initWithCoordinate:destinationCoordinate
         addressDictionary:nil];
        MKMapItem *destination=[[MKMapItem alloc]initWithPlacemark:destinationPlacemark];
        request.destination=destination;

        request.requestsAlternateRoutes=YES;
        MKDirections *directions=[[MKDirections alloc]initWithRequest:request];
        NSLog(@" ABOUT TO CALCULATE DIRECTIONS");
        [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse
        *response, NSError *error) {
            if (!error){
                NSLog(@" calculating directions SUCCESSFULL");
                [self showRoute:response];
            }
            else{
                NSLog(@"error in calculating directions with completion handler \n%@",
            [error userInfo]);
            }
        }];

    }
    -(void)showRoute : (MKDirectionsResponse *) response{
        NSLog(@"showRoute function executing");
        for (MKRoute *route in response.routes) {
            [self.myMapView addOverlay:route.polyline level:MKOverlayLevelAboveRoads];
        }
    }
    -(MKOverlayRenderer*)mapView:(MKMapView *)mapView rendererForOverlay:
       (id<MKOverlay>)overlay{
        NSLog(@" RENDERER FOR OVERLAY STARTED");
        MKPolyline *route=overlay;
        MKPolylineRenderer *renderer=[[MKPolylineRenderer alloc]initWithPolyline:route];
        renderer.strokeColor=[UIColor blueColor];
        //renderer.lineWidth=5.0;
        return renderer;


    }

    /*-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation 
       *)userLocation
    {   NSLog(@" DID UPDATE USER LOCATION STARTED");
        CLLocationCoordinate2D myLocation=[userLocation coordinate];
        MKCoordinateRegion zoomRegion=MKCoordinateRegionMakeWithDistance(myLocation, 2500, 
          2500);

        [self.myMapView setRegion:zoomRegion animated:YES];
    }
    */
    /*-(BOOL)shouldAutorotateToInterfaceOrientation:
         (UIInterfaceOrientation)toInterfaceOrientation
    {
        return (toInterfaceOrientation != UIInterfaceOrientationMaskPortraitUpsideDown);

    }
     */


    - (IBAction)button:(id)sender {
        NSString *url=@"http://maps.apple.com/maps?daddr=40.707184,-73.998392";
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

    }

    @end
Community
  • 1
  • 1

1 Answers1

2

Two issues:

  1. If you meant to have the coordinates be in CA, the sign for the longitude is wrong. I suspect you intended 37.33181, -122.03118.

  2. The source and destination coordinates are very, very close to each other (just a few feet apart in Apple's Cupertino parking lot). You need coordinates further apart from each other to get meaningful directions.

    If you intended your destination to be Stanford, use something more like 37.4300, -122.1700.

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Ya so sorry i forgot the minus sign. But I tried it , doesnt work. map shown in map view is a zoomed out america's map with a blue dot blinking on california. – Aseem hegshetye Aug 23 '14 at 04:15
  • When you add the route as a `MKPolyLine` overlay to your map, it doesn't change the map's `region`. You have to do that yourself (explicit set `region`, use `MKMapCamera`, etc.). – Rob Aug 23 '14 at 10:17