IOS-OC-高德地圖根據兩個座標點繪製路線__IOS

來源:互聯網
上載者:User



因為做發車項目 用到高德地圖,不過兩個項目高德地圖 百度地圖都用到過,基本流程都一樣。


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;


}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.