MKMapView绘制路线
来源:互联网 发布:家用网络监控 编辑:程序博客网 时间:2024/06/08 07:02
#import "ViewController.h"#import <MapKit/MapKit.h>@interface ViewController ()<MKMapViewDelegate>@property (weak, nonatomic) IBOutlet MKMapView *mapView;@property (nonatomic, strong) CLGeocoder *geocoder;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; //初始化地理编码对象 self.geocoder = [CLGeocoder new]; //设置代理 self.mapView.delegate = self;}//添加遮盖物- (IBAction)addOverlay:(id)sender { //1.给定起点和终点 NSString *startPoint = @"北京"; NSString *endPoint = @"深圳"; //2.使用地理编码把名字转成经纬度值 [self.geocoder geocodeAddressString:startPoint completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { //取地标数组最后一项(假定服务器返回一个) CLPlacemark *startPlacemark = [placemarks lastObject]; //3.创建大头针对象,设置属性;添加 MKPointAnnotation *annotation = [MKPointAnnotation new]; annotation.coordinate = startPlacemark.location.coordinate; annotation.title = @"起点"; [self.mapView addAnnotation:annotation]; //一定写在block内部 [self.geocoder geocodeAddressString:endPoint completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { CLPlacemark *endPlacemark = [placemarks lastObject]; MKPointAnnotation *annotation = [MKPointAnnotation new]; annotation.coordinate = endPlacemark.location.coordinate; annotation.title = @"终点"; [self.mapView addAnnotation:annotation]; [self startRoute:startPlacemark withPlacemark:endPlacemark]; }]; }]; //4.画线(添加overlay)}- (void)startRoute:(CLPlacemark *)startPlacemark withPlacemark:(CLPlacemark *)endPlacemark { MKMapItem *sourceItem = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithPlacemark:startPlacemark]]; MKMapItem *targetItem = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithPlacemark:endPlacemark]]; //创建request对象 MKDirectionsRequest *request = [MKDirectionsRequest new]; //给request设置起点和终点 request.source = sourceItem; request.destination = targetItem; //创建MKDirections对象; 发送请求 MKDirections *directions = [[MKDirections alloc] initWithRequest:request]; [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * _Nullable response, NSError * _Nullable error) { if (!error) { //取出整体路线; 取出对应路线的所有steps;添加polyline for (MKRoute *route in response.routes) { NSLog(@"总路程:%f千米; 预计的总时间:%f小时",route.distance/1000, route.expectedTravelTime/3600); //第一处可以添加几何线 //steps for (MKRouteStep *step in route.steps) { NSLog(@"每个step描述:%@; step距离:%f", step.instructions, step.distance); //第二处可以添加几何线到地图视图上 [self.mapView addOverlay:step.polyline]; } } } }];}//设置线的颜色和粗细- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay { MKPolylineRenderer *render = [[MKPolylineRenderer alloc] initWithOverlay:overlay]; render.lineWidth = 5.0; render.strokeColor = [UIColor blueColor]; return render;}@end
0 0
- MKMapView绘制路线
- MKMapView类——获取并绘制两点之间的路线
- MKMapView简单的路线绘画
- swift地图定位(十八)发送网络请求给苹果服务器获取导航路线并解析和绘制到MKMapView
- iOS MKMapView显示地址及路线
- MKMapView
- 简易绘制地图路线
- MKDirections绘制导航路线
- 地图绘制路线
- 使用谷歌地图api绘制路线
- Unity LineRenderer绘制物体行走路线
- 百度地图api绘制路线规划实例
- MKMapView用法
- iphone--MKMapView
- iphone MKMapView
- MKMapView展示
- MKMapView 相关
- MKMapView用法
- [从头学绘画] 第18节 六十四式八卦掌 (49-56)
- GIS开源库shapeLib的使用方法
- 线程_ioin_detach
- js中setAttribute 的兼容性
- 快速网页分析-page_parser
- MKMapView绘制路线
- HDU 5293 Tree chain problem(数链问题)【LCA+树形dp+dfs序+树状数组】
- hdu 1907 John
- Mybatis学习总结
- iOS10适配推送中的报错
- Linux epoll机制初识
- 小小英雄隐私政策
- easyui combobox可编辑的情况下,只能首字母开始过滤的问题选项
- C++检测标点符号