因为做发车项目 用到高德地图,不过两个项目高德地图 百度地图都用到过,基本流程都一样。
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;
}