出于学习目的,我正在尝试开发一个应用程序,该应用程序将显示到 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”。
非常感谢帮助!