因為做發車項目 用到高德地圖,不過兩個項目高德地圖 百度地圖都用到過,基本流程都一樣。
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;
}