3

出于学习目的,我正在尝试开发一个应用程序,该应用程序将显示到 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

我只有一个带有地图视图和以下代码的视图控制器:

路由AppViewController.h

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

路由AppViewController.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”。
非常感谢帮助!

4

1 回答 1

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...
             }
于 2013-12-28T18:29:14.047 回答