ios- 地图路线规划
生活随笔
收集整理的這篇文章主要介紹了
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- 地图路线规划的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何写学术论文的rebuttal?
- 下一篇: 该如何提升自己的编程能力?