【问题标题】:MKDirections not showing route on map in iOSMKDirections 在 iOS 中未在地图上显示路线
【发布时间】:2014-06-23 07:57:28
【问题描述】:

我正在开发一个应用程序,我需要显示两个坐标之间的路线方向。我使用了MKDirections 并传递了两个坐标作为源和目标,但是在mapView 上它没有显示任何路线或绘制任何折线。下面是我的代码。在MKDirections 中,它总是显示nil。请让我知道我做错了什么。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.activityIndicator.hidden = YES;
    self.routeDetailsButton.hidden = YES;
    self.routeDetailsButton.enabled = NO;
    self.mapView.delegate = self;
    self.mapView.showsUserLocation = YES;
    self.navigationItem.title = @"RouteMaster";

}

#pragma mark - MapKit delegate methods

- (void)mapView:(MKMapView *)aMapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    CLLocationCoordinate2D loc = [userLocation.location coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 1000.0f, 1000.0f);
    [self.mapView setRegion:region animated:YES];
}


- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (IBAction)handleRoutePressed:(id)sender {
    // We're working

    CLLocationCoordinate2D sourceCoords = CLLocationCoordinate2DMake(28.6100, 77.2300);
    MKPlacemark *sourcePlacemark = [[MKPlacemark alloc] initWithCoordinate:sourceCoords addressDictionary:nil];

    MKMapItem *srcMapItem = [[MKMapItem alloc]initWithPlacemark:sourcePlacemark];

    CLLocationCoordinate2D destinationCoords = CLLocationCoordinate2DMake(18.9750, 72.8258);
    MKPlacemark *destinationPlacemark = [[MKPlacemark alloc] initWithCoordinate:destinationCoords addressDictionary:nil];

    MKMapItem *distMapItem = [[MKMapItem alloc]initWithPlacemark:destinationPlacemark];

    MKDirectionsRequest *request = [[MKDirectionsRequest alloc]init];
    [request setSource:srcMapItem];
    [request setDestination:distMapItem];

    MKDirections *direction = [[MKDirections alloc]initWithRequest:request];
    [direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {

         if (error)
             NSLog(@"Error %@", error.description);
        else
            NSLog(@"response = %@",response);
        NSArray *arrRoutes = [response routes];
        [arrRoutes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

            MKRoute *rout = obj;

            MKPolyline *line = [rout polyline];
            [self.mapView addOverlay:line];
            NSLog(@"Rout Name : %@",rout.name);
            NSLog(@"Total Distance (in Meters) :%f",rout.distance);

            NSArray *steps = [rout steps];

            NSLog(@"Total Steps : %lu",(unsigned long)[steps count]);

            [steps enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                NSLog(@"Rout Instruction : %@",[obj instructions]);
                NSLog(@"Rout Distance : %f",[obj distance]);
            }];
        }];
    }];

}

#pragma mark - Utility Methods
- (void)plotRouteOnMap:(MKRoute *)route
{
    if(_routeOverlay) {
        [self.mapView removeOverlay:_routeOverlay];
    }

    // Update the ivar
    _routeOverlay = route.polyline;

    // Add it to the map
    [self.mapView addOverlay:_routeOverlay];

}


#pragma mark - MKMapViewDelegate methods
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
    renderer.strokeColor = [UIColor redColor];
    renderer.lineWidth = 4.0;
    return  renderer;
}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay {

    if ([overlay isKindOfClass:[MKPolyline class]]) {
        MKPolylineView* aView = [[MKPolylineView alloc]initWithPolyline:(MKPolyline*)overlay] ;
        aView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];
        aView.lineWidth = 10;
        return aView;
    }
    return nil;
}

【问题讨论】:

  • 您不需要指定地址字典,但请注意 MKDirections 并不支持世界各地的路线。此代码必须显示错误“MKErrorDomain Code=5 Directions Not Available”。使用美国或欧洲的坐标测试此代码。

标签: ios iphone mkmapview mapkit


【解决方案1】:

大多数情况下,如果 [_mapView setRegion:region] 区域和折线路径路径不在同一区域上(两者都应显示在同一屏幕上),则会发生此错误。 就我而言,当我所在的地区是印度时,我正在寻找美国的路线路径。所以它不会调用 -(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id)overlay;功能。

【讨论】:

    【解决方案2】:

    添加这个方法

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

    【讨论】:

    • 我已经补充说,但是它没有被绘制,我认为它没有进入块语句,我需要为 MKOverlayRenderer 分配任何委托。
    • 它没有显示任何错误,它没有在地图视图上绘制或显示方向
    • 在calculateDirectionsWithCompletionHandler之后添加 if (error) NSLog(@"Error %@", error.description);
    • 我已经用源代码编辑了我的问题。请让我知道我做错了什么。它甚至没有进入错误语句。
    • addressdictionary 不能为 nil 你必须传递地址
    猜你喜欢
    • 1970-01-01
    • 2013-09-19
    • 2018-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多