在 MapView 上绘制 MKPolyline 显示两个位置之间的直线

Posted

技术标签:

【中文标题】在 MapView 上绘制 MKPolyline 显示两个位置之间的直线【英文标题】:Draw a MKPolyline on a MapView shows straight line between both locations 【发布时间】:2016-07-01 11:33:11 【问题描述】:

如何获取位置,如显示位置之间确切路径的谷歌地图。因为它显示了直接的直线,它不包含两个地方之间的任何路径,也没有显示它通过的它们周围的位置。 我使用了以下代码:

CLLocationCoordinate2D coordinateArray[2];
    coordinateArray[0] = CLLocationCoordinate2DMake(51.5074, 0.1278);
    coordinateArray[1] = CLLocationCoordinate2DMake(48.8566, 2.3522);


    self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:2];
    [self.mapView setVisibleMapRect:[self.routeLine boundingMapRect]]; //If you want the route to be visible

    [self.mapView addOverlay:self.routeLine];

对于这两个路径的连接,此代码已经实现。

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay

    if(overlay == self.routeLine)
    
        if(nil == self.routeLineView)
        
            self.routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine];
            self.routeLineView.fillColor = [UIColor redColor];
            self.routeLineView.strokeColor = [UIColor redColor];
            self.routeLineView.lineWidth = 5;

        

        return self.routeLineView;
    

    return nil;

【问题讨论】:

【参考方案1】:

试试我的代码一:

- (void)drawRouteFromLocation:(LocationPin *)source toLocation:(LocationPin *)destination


    [self removeCurrentRouteDrawing];

    MKPlacemark *sourcePlaceMark = [[MKPlacemark alloc]initWithCoordinate:source.actualCoordinate addressDictionary:nil];
    MKPlacemark *destinationPlaceMark = [[MKPlacemark alloc]initWithCoordinate:destination.actualCoordinate addressDictionary:nil];

    MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];

    request.source = [[MKMapItem alloc] initWithPlacemark:sourcePlaceMark];
    request.destination = [[MKMapItem alloc] initWithPlacemark:destinationPlaceMark];
    request.requestsAlternateRoutes = NO;
    MKDirections *directions = [[MKDirections alloc] initWithRequest:request];

    [directions calculateDirectionsWithCompletionHandler:
     ^(MKDirectionsResponse *response, NSError *error) 
         if (error)
         
             // Handle Error
         
         else
         
             [self showRoute:response];
         
     ];


-(void)showRoute:(MKDirectionsResponse *)response

    for (MKRoute *route in response.routes)
    
        [self.mkMapView addOverlay:route.polyline level:MKOverlayLevelAboveRoads];
//        for (MKRouteStep *step in route.steps)
//        
//            DEBUG_LOG(@"%@", step.instructions);
//        

    


#pragma mark - MKMapViewDelegate
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated

//    DEBUG_LOG(@"region Will Change");

    if ([self.delegate respondsToSelector:@selector(mapView:regionWillChangeAnimated:)])
    
        [self.delegate mapView:mapView regionWillChangeAnimated:animated];
    



- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated

//    DEBUG_LOG(@"region Did Change");

    if ([self.delegate respondsToSelector:@selector(mapView:regionDidChangeAnimated:)])
    
        [self.delegate mapView:mapView regionDidChangeAnimated:animated];
    



- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView

    DEBUG_LOG(@"mapView Did Finish Loading Map");
    self.mapInitialized = YES;

    if ([self.delegate respondsToSelector:@selector(mapViewDidFinishLoadingMap:)])
    
        [self.delegate mapViewDidFinishLoadingMap:mapView];
    


//- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
//
//    if ([overlay isKindOfClass: [MKPolyline class]])
//    
//        // This is for a dummy overlay to work around a problem with overlays
//        // not getting removed by the map view even though we asked for it to
//        // be removed.
//        MKOverlayView * dummyView = [[MKOverlayView alloc] init];
//        dummyView.alpha = 0.0;
//        return dummyView;
//    
//    else
//    
//        return nil;
//    
//

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation

    if ([self.delegate respondsToSelector:@selector(mapView:viewForAnnotation:)])
    
        return [self.delegate mapView:mapView viewForAnnotation:annotation];
    

    return nil;


- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay

    if ([overlay isKindOfClass:[MKPolyline class]])
    
        MKPolylineRenderer *renderer = [[ MKPolylineRenderer alloc]initWithOverlay:overlay];
        renderer.lineWidth = 10;
        renderer.strokeColor = [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0];
        [self _zoomToPolyLine:self.mkMapView polyLine:overlay animated:YES];
        return renderer;
    
    else
    
        return nil;
    



#pragma mark - Utils

-(void)_zoomToPolyLine:(MKMapView*)map polyLine:(MKPolyline*)polyLine animated:(BOOL)animated

//    MKPolygon* polygon =
//    [MKPolygon polygonWithPoints:polyLine.points count:polyLine.pointCount];
//    
//    [self.mkMapView setRegion:MKCoordinateRegionForMapRect([polygon boundingMapRect])
//          animated:animated];

    [map setVisibleMapRect:[polyLine boundingMapRect] edgePadding:UIEdgeInsetsMake(35.0, 35.0, 35.0, 35.0) animated:animated];


- (void)removeCurrentRouteDrawing

    if (self.mkMapView.overlays.count)
    
        [self.mkMapView removeOverlays:self.mkMapView.overlays];

    

【讨论】:

以上是关于在 MapView 上绘制 MKPolyline 显示两个位置之间的直线的主要内容,如果未能解决你的问题,请参考以下文章

在mapview iOS 6中绘制折线

使用 MKPolyline 在地图上绘制用户路线

添加 MKPolyLine NSInvalidArgumentException

在 MKPolyLine 视图中绘制渐变

如何向 MKPolyline 和 MKPolygon 添加描述?

如何在 Android 的 MapView 上绘制带边框的文本?