IOS-OC-高德地图根据两个坐标点绘制路线



因为做发车项目 用到高德地图,不过两个项目高德地图 百度地图都用到过,基本流程都一样。


1.导入需要的SDK配置。。。

2. 

// 代理

<MAMapViewDelegate, AMapLocationManagerDelegate,AMapSearchDelegate,AMapNaviDriveManagerDelegate, AMapNaviDriveViewDelegate>


// 属性

// 地图相关

@property (strong, nonatomic) MAMapView *mapView;

@property (strong, nonatomic) AMapLocationManager *locationManager;

@property (strong, nonatomic) MAPointAnnotation *annotation;       // 地图标注

@property (nonatomic, strong) AMapRoute *route;                     // 路线规划

// 导航

@property (nonatomic, strong) AMapNaviDriveManager *driveManager;

@property (nonatomic, strong) AMapNaviPoint *startPoint;

@property (nonatomic, strong) AMapNaviPoint *endPoint;



// 设置代理

 self.driveManager = [[AMapNaviDriveManager alloc] init];

 [self.driveManager setDelegate:self];


// 设置起始点坐标        

self.startPoint = [AMapNaviPoint locationWithLatitude:startLat longitude:startLon];

self.endPoint   = [AMapNaviPoint locationWithLatitude:endLat longitude:endLon];


    

// 进行路线规划

 [self.driveManager calculateDriveRouteWithStartPoints:@[self.startPoint]

                                                endPoints:@[self.endPoint]

                                                wayPoints:nil

                                          drivingStrategy:AMapNaviDrivingStrategySingleDefault];


#pragma mark - AMapNaviDriveManager Delegate 路线规划回调


- (void)driveManagerOnCalculateRouteSuccess:(AMapNaviDriveManager *)driveManager

{

    NSLog(@"onCalculateRouteSuccess");

    

    //算路成功后显示路径

    [self showNaviRoutes];

}


// 绘制路线

- (void)showNaviRoutes

{

    if ([self.driveManager.naviRoutes count] <= 0)

    {

        return;

    }

    

   [self.mapView removeOverlays:self.mapView.overlays];

    

    //将路径显示到地图上

    for (NSNumber *aRouteID in [self.driveManager.naviRoutes allKeys])

    {

        AMapNaviRoute *aRoute = [[self.driveManager naviRoutes] objectForKey:aRouteID];

        int count = (int)[[aRoute routeCoordinates] count];

        

        //添加路径Polyline

        CLLocationCoordinate2D coords[count];


        for (int i = 0; i < count; i++)

        {

            AMapNaviPoint *coordinate = [[aRoute routeCoordinates] objectAtIndex:i];

            coords[i].latitude = [coordinate latitude];

            coords[i].longitude = [coordinate longitude];

        }


        MAPolyline *polyline = [MAPolyline polylineWithCoordinates:coords count:count];


        [self.mapView addOverlay:polyline];

 

    }

    

    [self.mapView showAnnotations:self.mapView.annotations animated:NO];

    

 

 }


// 设置路线颜色

- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay

{

    

    if ([overlay isKindOfClass:[MAPolyline class]])

    {

        MAPolylineRenderer *polylineRenderer = [[MAPolylineRenderer alloc] initWithPolyline:overlay];

        polylineRenderer.strokeColor = [UIColor blueColor];

        polylineRenderer.lineWidth   = 5.f;

   

        

        

        return polylineRenderer;

    }

    

    return nil;


}




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值