使用mkdirections在加利福尼亚寻找方向

时间:2014-08-21 22:21:05

标签: ios objective-c mapkit

我正在使用     display route on iOS 7 maps: addOverlay has no effect     这可以在我的应用中嵌入地图。     我收到以下错误:     mkdirectionserrorcode = 6;     mkerrorgeoerror =" -403&#34 ;;     nslocalized description ="方向不可用&#34 ;;     NSlocalizedfailureReason ="无法确定到最近的道路的路线&#34 ;;

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

1 个答案:

答案 0 :(得分:2)

两个问题:

  1. 如果您打算让坐标位于CA中,则经度符号是错误的。我怀疑你打算37.33181,-122.03118。

  2. 源和目的地坐标非常非常接近(在Apple的库比蒂诺停车场只有几英尺)。您需要彼此远离的坐标才能获得有意义的指示。

    如果您的目的地是斯坦福,请使用更像37.4300,-122.1700的内容。