地图划线
2016-04-28 13:49
204 查看
#import "ViewController.h" #import <CoreLocation/CoreLocation.h> #import <MapKit/MapKit.h> @interface ViewController ()<MKMapViewDelegate> @property(nonatomic ,strong)MKMapView * map; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; MKMapView * map = [[MKMapView alloc]initWithFrame:self.view.bounds]; self.map = map; self.map.delegate = self; [self.view addSubview:map]; CLGeocoder * geoCode = [[CLGeocoder alloc]init]; [geoCode geocodeAddressString:@"北京" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { //通过coreLocation 的CLPlacemark 获取地面 装换为MKPlacemark MKPlacemark * place1 = [[MKPlacemark alloc]initWithPlacemark:[placemarks firstObject]]; [geoCode geocodeAddressString:@"武汉" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { MKPlacemark * place2 = [[MKPlacemark alloc]initWithPlacemark:[placemarks firstObject]]; //创建方向请求 MKDirectionsRequest * request = [[MKDirectionsRequest alloc]init]; request.source = [[MKMapItem alloc]initWithPlacemark:place1]; request.destination = [[MKMapItem alloc]initWithPlacemark:place2]; //创建方向对象 MKDirections * direction = [[MKDirections alloc]initWithRequest:request]; //计算方向 添加蒙版 也就是划线 [direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * _Nullable response, NSError * _Nullable error) { for (MKRoute * route in response.routes) { [self.map addOverlay:route.polyline]; } }]; }]; }]; } //当添加折线时,调用 , 返回一个折线渲染器 -(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay { MKPolylineRenderer * polylineRenderer = [[MKPolylineRenderer alloc]initWithPolyline:overlay]; polylineRenderer.lineWidth = 2; polylineRenderer.strokeColor = [UIColor orangeColor]; return polylineRenderer; }
相关文章推荐
- 《高性能javascript》 领悟随笔之-------DOM编程篇
- Java---练习(面试题) :字符串截取(2-最终版)
- Linux内核分析 期末总结
- Drupal8 社区文档之内容类型
- 数值工具类
- HelloWorld
- Light OJ-1082 - Array Queries,线段树区间查询最大值,哈哈,水过~~
- 解决apache mina在网络环境慢下的粘包问题
- OSI七层与TCP/IP五层网络架构详解
- Macbook上满足apt-get/yum的工具:Homebrew
- 自定义View——类似微信裁剪控件
- 【Red5】如何搭建环境以及调试Red5(1.0.6版本)应用(三)
- server 同一服务器上的两个php.ini文件!!
- 端到端的数据
- 异步任务
- Nginx漏洞利用与安全加固
- 门面设计模式
- Drupal8社区文档之用户、权限、角色
- 使用RawSocket进行网络抓包
- JAVA科学计数法,金额数字,转换为中文大写字