• 地图划线


    #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;
        
        
    }
    

      

  • 相关阅读:
    Java 使用 EasyExcel 实现简单的读写操作
    Java上传文件到阿里云对象存储器OSS
    Springboot 项目解决跨域的问题
    Java 使用 Kafka 发布信息与消费消息
    安装PHPldapAdmin出现You don't have permission to access /phpldapadmin/ on this server.问题
    LDAP安装、LDAP数据迁移、LDAP卸载指南及PHPldapAdmin管理软件安装
    LDAP数据备份与数据恢复
    docker 启动所有镜像
    详解GET 和 POST请求的本质区别
    如何使用 markdown
  • 原文地址:https://www.cnblogs.com/yuwei0911/p/5442401.html
Copyright © 2020-2023  润新知