Objective c MKDirectionsResponse:错误,方向不可用。(美国)
出于学习目的,我正在尝试开发一个应用程序,该应用程序将显示指向MKMapView上特定点的方向。Objective c MKDirectionsResponse:错误,方向不可用。(美国),objective-c,cocoa-touch,mkmapview,mapkit,clgeocoder,Objective C,Cocoa Touch,Mkmapview,Mapkit,Clgeocoder,出于学习目的,我正在尝试开发一个应用程序,该应用程序将显示指向MKMapView上特定点的方向。 但是,无论地址如何,DirectionResponse每次都会给我以下错误: 2013-12-28 17:52:23.100 routingApp[378:70b] ERROR 2013-12-28 17:52:23.102 routingApp[378:70b] Directions Not Available 我只有一个带有地图视图和以下代码的视图控制器: routingAppViewCont
但是,无论地址如何,DirectionResponse每次都会给我以下错误:
2013-12-28 17:52:23.100 routingApp[378:70b] ERROR
2013-12-28 17:52:23.102 routingApp[378:70b] Directions Not Available
我只有一个带有地图视图和以下代码的视图控制器:
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;
}
#导入“routingAppViewController.h”
@接口路由AppViewController()
@结束
@实现路由应用程序视图控制器
-(无效)viewDidLoad
{
[超级视图下载];
NSString*位置=@“385 Mid Avenue Weston”;
_mapView.showsUserLocation=是;
[自我说明:位置];
}
-(void)getDirections:(NSString*)地址{
CLGeocoder*geocoder=[[CLGeocoder alloc]init];
[geocoder geocodeAddressString:地址
completionHandler:^(NSArray*放置标记,NSError*错误){
//检查返回的位置标记
if(placemarks&&placemarks.count>0){
CLPlacemark*topResult=[placemarks objectAtIndex:0];
//创建MLPlacemark并将其添加到地图视图中
MKPlacemark*placemark=[[MKPlacemark alloc]initWithPlacemark:topResult];
[self.mapView addAnnotation:placemark];
_目的地=地点标记;
}
}];
MKMapItem*mapItem=[[MKMapItem alloc]initWithPlacemark:_destination];
MKDirectionsRequest*请求=[[MKDirectionsRequest alloc]init];
request.source=[MKMapItem-mapItemForCurrentLocation];
request.destination=mapItem;
request.requestSalterRoutes=否;
MKDirections*directions=[[MKDirections alloc]initWithRequest:request];
[方向使用CompletionHandler计算方向:
^(MKDirectionsResponse*响应,N错误*错误){
如果(错误){
NSLog(@“错误”);
NSLog(@“%@,[错误本地化描述]);
}否则{
[自我展示路线:回应];
}
}];
}
-(无效)showRoute:(MKDirectionsResponse*)响应
{
for(MKRoute*响应中的route.routes)
{
[\u地图视图
addOverlay:route.polyline级别:mkOverlayLevel高于道路];
用于(MKRouteStep*路径中的步骤。步骤)
{
NSLog(@“%@”,步骤说明);
}
}
}
-(MKOverlayRenderer*)地图视图:(MKMapView*)地图视图渲染器ForOverlay:(id)overlay
{
MKPolylineRenderer*渲染器=
[[MKPolylineRenderer alloc]initWithOverlay:overlay];
renderer.strokeColor=[UIColor blueColor];
渲染器。线宽=5.0;
返回渲染器;
}
错误在getDirections
方法中给出。我在谷歌上搜索了一下,大部分错误意味着这个国家没有方向服务。但是我使用的是美国地址,所以我不明白为什么它不起作用 注释添加正确,我的位置设置为“Apple”。
非常感谢您的帮助 由
geocodeAddressString
调用的completionHandler
块是异步的
这意味着试图获取从当前位置到\u目的地
的方向的代码会在geocodeAddressString
行之后立即运行,并在设置\u目的地
之前执行
因此,当创建mapItem
时,它正在使用\u destination
仍设置为nil
(地理编码器块尚未完成并设置\u destination
)
尝试获取从当前位置到nil
的方向会导致获取方向错误
最简单的修复方法是将获取方向请求的代码移动到
geocodeAddressString
的completionHandler
块中(实际设置了\u 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...
}