MKDirectionsResponse:错误,方向不可用。 (美国)

时间:2013-12-28 17:02:51

标签: objective-c cocoa-touch mkmapview mapkit clgeocoder

出于学习目的,我正在尝试开发一个应用程序,它将显示MKMapView上特定点的方向 但是,DirectionsResponse每次都会给我以下错误,无论地址如何:

2013-12-28 17:52:23.100 routingApp[378:70b] ERROR
2013-12-28 17:52:23.102 routingApp[378:70b] Directions Not Available

我只有一个带有地图视图的View Controller和以下代码:

routingAppViewController.h

@interface routingAppViewController : UIViewController
@property (strong, nonatomic) IBOutlet MKMapView *mapView;
@property (strong, nonatomic) MKPlacemark *destination;
@end

routingAppViewController.m

#import "routingAppViewController.h"

@interface routingAppViewController ()
@end

@implementation routingAppViewController

- (void)viewDidLoad
{
[super viewDidLoad];
NSString *Location = @"385 Mid Avenue Weston";
_mapView.showsUserLocation = YES;
[self getDirections:Location];


}

-(void)getDirections:(NSString *)address{


CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:address
             completionHandler:^(NSArray* placemarks, NSError* error){
                 // Check for returned placemarks
                 if (placemarks && placemarks.count > 0) {
                     CLPlacemark *topResult = [placemarks objectAtIndex:0];
                     // Create a MLPlacemark and add it to the map view
                     MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
                     [self.mapView addAnnotation:placemark];
                     _destination = placemark;
                 }
             }];

MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:_destination];


MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
request.source = [MKMapItem mapItemForCurrentLocation];

request.destination = mapItem;
request.requestsAlternateRoutes = NO;

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

[directions calculateDirectionsWithCompletionHandler:
 ^(MKDirectionsResponse *response, NSError *error) {
     if (error) {
         NSLog(@"ERROR");
         NSLog(@"%@",[error localizedDescription]);
     } else {
         [self showRoute:response];
     }
 }];


}

-(void)showRoute:(MKDirectionsResponse *)response
{
for (MKRoute *route in response.routes)
{
    [_mapView
     addOverlay:route.polyline level:MKOverlayLevelAboveRoads];

    for (MKRouteStep *step in route.steps)
    {
        NSLog(@"%@", step.instructions);
    }
}
}

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay
{
MKPolylineRenderer *renderer =
[[MKPolylineRenderer alloc] initWithOverlay:overlay];
renderer.strokeColor = [UIColor blueColor];
renderer.lineWidth = 5.0;
return renderer;
}

错误在getDirections方法中给出 我用谷歌搜索它,大多数错误意味着这个国家没有方向服务。但是我使用的是美国地址,所以我不明白为什么它不起作用。

正确添加注释,我的位置设置为“Apple” 非常感谢!

1 个答案:

答案 0 :(得分:4)

completionHandler调用的geocodeAddressString块是异步的。

这意味着尝试获取从当前位置到_destination的路线的代码会在geocodeAddressString行之后立即运行并在之前执行 > _destination已设置。

因此,当创建mapItem时,它仍然使用_destination设置为nil(地理编码器块尚未完成并设置_destination)。

尝试从当前位置获取路线nil会导致获取路线错误。


最简单的解决方法是将completionHandler geocodeAddressString _destination块中获取路线请求的代码移动(实际设置 [self.mapView addAnnotation:placemark]; _destination = placemark; //move the code that gets the directions here, //inside this block... MKMapItem *mapItem = [[MKMapItem alloc] init... ... [directions calculateDirections... } 后):

{{1}}