You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Apple Map上绘制两个标注点间的道路路线折线?

嘿,看起来你已经搭好了MapKit的基础框架配置,接下来咱们一步步实现两个标注点之间的真实道路路线折线,完全贴合你的TrackViewController场景:

步骤1:添加起点&终点标注(如果还没完成的话)

先给地图添加上起点和终点的标注点,你可以写一个单独的方法来完成这件事:

- (void)addStartAndEndAnnotations {
    // 起点标注(替换成你实际的坐标)
    MKPointAnnotation *startAnnotation = [[MKPointAnnotation alloc] init];
    startAnnotation.coordinate = CLLocationCoordinate2DMake(37.7749, -122.4194);
    startAnnotation.title = @"起点";
    [self.mapView addAnnotation:startAnnotation];
    
    // 终点标注(替换成你实际的坐标)
    MKPointAnnotation *endAnnotation = [[MKPointAnnotation alloc] init];
    endAnnotation.coordinate = CLLocationCoordinate2DMake(37.3352, -122.0322);
    endAnnotation.title = @"终点";
    [self.mapView addAnnotation:endAnnotation];
}
步骤2:请求真实道路路线数据

咱们要用苹果的MKDirections服务来获取道路路线,而不是直接画直线——这样才能得到贴合实际道路的折线。你可以在viewDidLoad或者按钮点击事件里调用这个方法:

- (void)requestRoadRouteBetweenPoints {
    // 先拿到已添加的起点和终点标注(如果是直接用坐标的话,可以跳过这一步直接传坐标)
    if (self.mapView.annotations.count < 2) {
        NSLog(@"请先添加起点和终点标注哦");
        return;
    }
    MKPointAnnotation *startAnnotation = self.mapView.annotations[0];
    MKPointAnnotation *endAnnotation = self.mapView.annotations[1];
    
    // 构建路线请求
    MKDirectionsRequest *routeRequest = [[MKDirectionsRequest alloc] init];
    routeRequest.source = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:startAnnotation.coordinate addressDictionary:nil]];
    routeRequest.destination = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:endAnnotation.coordinate addressDictionary:nil]];
    routeRequest.transportType = MKDirectionsTransportTypeAutomobile; // 可选:步行/公交/驾车,按需切换
    
    // 发起路线请求
    MKDirections *directions = [[MKDirections alloc] initWithRequest:routeRequest];
    [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
        if (error) {
            NSLog(@"路线请求失败:%@", error.localizedDescription);
            return;
        }
        
        // 取第一条路线(如果有多条备选路线,可遍历response.routes添加)
        MKRoute *targetRoute = response.routes.firstObject;
        // 将路线折线添加到地图上
        [self.mapView addOverlay:targetRoute.polyline level:MKOverlayLevelAboveRoads];
        
        // 可选:自动调整地图视野,让整条路线完整显示
        MKMapRect routeBounds = targetRoute.polyline.boundingMapRect;
        [self.mapView setVisibleMapRect:routeBounds edgePadding:UIEdgeInsetsMake(50, 50, 50, 50) animated:YES];
    }];
}
步骤3:实现代理方法渲染折线

要让折线在地图上显示出来,必须实现MKMapViewDelegate的渲染方法,自定义折线的样式:

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
    if ([overlay isKindOfClass:[MKPolyline class]]) {
        MKPolylineRenderer *routeRenderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
        routeRenderer.strokeColor = [UIColor systemBlueColor]; // 折线颜色
        routeRenderer.lineWidth = 3.0; // 折线宽度
        routeRenderer.alpha = 0.8; // 透明度
        return routeRenderer;
    }
    return nil;
}
关键注意事项
  • 别忘了在viewDidLoad里设置代理:self.mapView.delegate = self;,不然代理方法不会生效。
  • 确保你的Info.plist里添加了位置权限描述(比如NSLocationWhenInUseUsageDescription),方向服务需要这个权限配置才能正常工作。
  • 如果需要支持多种交通方式,只需要修改routeRequest.transportType的枚举值即可。

内容的提问来源于stack exchange,提问作者Anupam Das

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:59:10