日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ios- 地图路线规划

發布時間:2023/12/14 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ios- 地图路线规划 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
地圖路線的繪制是很復雜的 我們需要包裝很多結構體 一層接一層 1.首先我們要給出兩個位置 起點和終點 的經緯度 2.給出起點和終點的詳細信息 3.包裝 起點的節點 和終點的節點 4.進行路線請求 5.發送請求 6.計算 我們可以取出路線 添加大頭針和遮蓋物來顯示我們 起點和終點之間是怎么樣連線的要實現系統的代理方法 畫線條 #import "ViewController.h" #import <MapKit/MapKit.h>@interface ViewController ()<MKMapViewDelegate>/*** 地圖顯示*/@property (nonatomic, weak) MKMapView *mapView;@end@implementation ViewController- (MKMapView *)mapView {if (!_mapView) {MKMapView *mapView = [[MKMapView alloc]initWithFrame:self.view.bounds];mapView.delegate = self;[self.view addSubview:mapView];//經緯度CLLocationCoordinate2D coordinate2D = {39.9087607478,116.3975780499};//比例尺MKCoordinateSpan span = {.01,.01};//設置范圍//是否顯示用戶的當前位置 // mapView.showsUserLocation = YES;MKCoordinateRegion region = MKCoordinateRegionMake(coordinate2D, span);[mapView setRegion:region];_mapView = mapView;}return _mapView; }- (void)viewDidLoad {[super viewDidLoad];[self mapView];// [self test];[self drawLine];// Do any additional setup after loading the view, typically from a nib. }/*** 測試*/- (void)test {CLLocationCoordinate2D first = {1,1};CLLocationCoordinate2D second = {2,2};CLLocationCoordinate2D coordinate[2] = {first,second};MKPolyline *line = [MKPolyline polylineWithCoordinates:coordinate count:2];[self.mapView addOverlay:line]; }- (void)drawLine {//起點和終點的經緯度CLLocationCoordinate2D start = {39.9087607478,116.3975780499};CLLocationCoordinate2D end = {40.9087607478,117.3975780499};//起點終點的詳細信息MKPlacemark *startPlace = [[MKPlacemark alloc]initWithCoordinate:start addressDictionary:nil];MKPlacemark *endPlace = [[MKPlacemark alloc]initWithCoordinate:end addressDictionary:nil];//起點 終點的 節點MKMapItem *startItem = [[MKMapItem alloc]initWithPlacemark:startPlace];MKMapItem *endItem = [[MKMapItem alloc]initWithPlacemark:endPlace];//路線請求MKDirectionsRequest *request = [[MKDirectionsRequest alloc]init];request.source = startItem;request.destination = endItem;//發送請求MKDirections *directions = [[MKDirections alloc]initWithRequest:request];__block NSInteger sumDistance = 0;//計算[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {if (!error) {//取出一條路線MKRoute *route = response.routes[0];//關鍵節點for(MKRouteStep *step in route.steps){//大頭針MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];annotation.coordinate = step.polyline.coordinate;annotation.title = step.polyline.title;annotation.subtitle = step.polyline.subtitle;//添加大頭針[self.mapView addAnnotation:annotation];//添加路線[self.mapView addOverlay:step.polyline];//距離sumDistance += step.distance;}NSLog(@"總距離 %ld",sumDistance);}}];}#pragma mark - 添加覆蓋物 - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {//繪制線條if ([overlay isKindOfClass:[MKPolyline class]]){MKPolylineRenderer *polylineRender = [[MKPolylineRenderer alloc]initWithPolyline:overlay];polylineRender.strokeColor = [UIColor redColor];polylineRender.lineWidth = 5;return polylineRender;}return nil; }- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated. }@end

總結

以上是生活随笔為你收集整理的ios- 地图路线规划的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。