苹果地图上的方向

时间:2015-07-04 05:29:24

标签: ios objective-c mkmapview core-location

我们可以使用一组点来获得苹果地图上的方向。以下代码段是绘制直线而不是方向

- (IBAction)getRoute:(id)sender {


[self.mapView removeOverlay:self.routeLine];

CLLocationCoordinate2D overflowLotCoords[5]={
    CLLocationCoordinate2DMake(39.04864351611461,-76.8513227245313),
    CLLocationCoordinate2DMake(39.04851710015167,-76.8517540587399),

    CLLocationCoordinate2DMake(39.04695987529381,-76.85235192135768),
    CLLocationCoordinate2DMake(39.04734847050665,-76.85236298239703),
    CLLocationCoordinate2DMake(39.04779491740192,-76.85232236959109)

};



MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:overflowLotCoords count:4];
[self.mapView addOverlay:polyLine];
self.routeLine = polyLine;

}

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
if ([overlay isKindOfClass:[MKPolyline class]]) {
    MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
    [renderer setStrokeColor:[UIColor redColor]];
    [renderer setLineWidth:4.0];
    [renderer setStrokeColor:[UIColor redColor]];
    return renderer;
}
return nil;
}

我想使用一组点绘制路线,我确实有源和目的地以及点列表。绘制路线后,我会使用webservice获取当前位置,并希望显示路线上的位置。

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以尝试这种方式并设置坐标:

 - (void)viewDidLoad {
        [super viewDidLoad];

        // center map
        CLLocationCoordinate2D startCoord = CLLocationCoordinate2DMake(47.081012, 2.398781);
        MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:MKCoordinateRegionMakeWithDistance(startCoord, 3000000, 3000000)];
        [self.mapView setRegion:adjustedRegion animated:YES];

        [self showLines];
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
    }

    - (void)showLines {
        CLLocationCoordinate2D *pointsCoordinate = (CLLocationCoordinate2D *)malloc(sizeof(CLLocationCoordinate2D) * 3);
        pointsCoordinate[0] = CLLocationCoordinate2DMake(48.856614, 2.352221);
        pointsCoordinate[1] = CLLocationCoordinate2DMake(45.764043, 4.835658);
        pointsCoordinate[2] = CLLocationCoordinate2DMake(43.296482, 5.369779);


        MKPolyline *polyline = [MKPolyline polylineWithCoordinates:pointsCoordinate count:3];
        free(pointsCoordinate);

        [self.mapView addOverlay:polyline];
    }


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

        // create a polylineView using polyline overlay object
        MKPolylineRenderer *polylineView = [[MKPolylineRenderer alloc] initWithPolyline:overlay];

// Custom polylineView
        polylineView.strokeColor =  [UIColor orangeColor];   
        polylineView.lineWidth = 2.0;
        polylineView.alpha = 0.5;

        return polylineView;
    }