MapKit_Code_路线

MapKit_Code_路线

#import "ViewController.h"
#import <MapKit/MapKit.h>
#import "RNAnnotation.h"

@interface ViewController ()<MKMapViewDelegate]]>

@property (nonatomic, strong) MKMapView *mapView;
@property (nonatomic, strong) CLGeocoder *geoCoder;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1.创建地图
    MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    mapView.delegate = self;
    self.mapView = mapView;
    [self.view addSubview:mapView];
    
    // 创建地理编码
    [self creatGeoCoder];
    
    [self.geoCoder geocodeAddressString:@"唐山" completionHandler:^(NSArray *placemarks, NSError *error) {
        
        CLPlacemark *tsPM = [placemarks firstObject];
        // 1. 添加大头针
        RNAnnotation *annotation = [[RNAnnotation alloc] init];
        annotation.title = tsPM.locality;
        annotation.subtitle = tsPM.name;
        annotation.coordinate = tsPM.location.coordinate;
        [self.mapView addAnnotation:annotation];
        
        [self.geoCoder geocodeAddressString:@"北京" completionHandler:^(NSArray *placemarks, NSError *error) {
             CLPlacemark *bjPM = [placemarks firstObject];
            
            RNAnnotation *annotation = [[RNAnnotation alloc] init];
            annotation.title = bjPM.locality;
            annotation.subtitle = bjPM.name;
            annotation.coordinate = bjPM.location.coordinate;
            [self.mapView addAnnotation:annotation];
           
            [self drawLineWithSourceCLPM:tsPM DestinationCLPM:bjPM];
            
        }];
    }];
    
}

- (void)drawLineWithSourceCLPM:(CLPlacemark *)sourceCLPM DestinationCLPM:(CLPlacemark *)destinationCLPM
{
    if (sourceCLPM == nil || destinationCLPM == nil) {
        return;
    }
    // 2. 设置方向请求
    MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
    
    // 3. 设置请求的起点
    // MKPlacemark 继承自 CLPlacemark
    // @interface MKPlacemark : CLPlacemark <MKAnnotation>
    MKPlacemark *sourcePM = [[MKPlacemark alloc] initWithPlacemark:sourceCLPM] ;
    request.source = [[MKMapItem alloc] initWithPlacemark:sourcePM];
    
    // 4. 设置请求的终点
    MKPlacemark *destinationPM = [[MKPlacemark alloc] initWithPlacemark:destinationCLPM];
    request.destination = [[MKMapItem alloc] initWithPlacemark:destinationPM];
    
    // 5. 根据请求创建方向
    MKDirections *direction = [[MKDirections alloc] initWithRequest:request];
    
    // 6.执行请求
    [direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
        
        if (error) {
            return ;
        }
        
        for (MKRoute *route in response.routes) {
            
            // 添加了遮盖,route路线的多段线polyline
            [self.mapView addOverlay:route.polyline];
        }
    }];
    
    // 遮盖 overlay
    
}


// 绘制遮盖调用代理方法
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    // 多线段的描述器
    MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
    // 线段的宽度
    renderer.lineWidth = 3;
    // 线段的颜色
    renderer.strokeColor = [UIColor redColor];
    return  renderer;
}


// 创建地理编码方法
- (void)creatGeoCoder
{
    if (!_geoCoder) {
        
        self.geoCoder = [[CLGeocoder alloc] init];
    }
}

@end





———————RNAnnotation.h 

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface RNAnnotation : NSObject <MKAnnotation]]>

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
// Title and subtitle for use by selection UI.
@property (nonatomic,  copy) NSString *title;
@property (nonatomic,  copy) NSString *subtitle;

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值