iOS:MKMapView导航画线

本文详细介绍如何使用MKMapView在iOS应用中实现地图功能,包括自定义标注图标、创建导航路径及在地图上绘制路线。通过创建自定义标注图标类并利用MKMapView的代理方法,实现了从地址到经纬度的转换,进而添加起点与终点并在地图上画出导航路径。

说明:

MKMapView是地图控件,支持在地图上画导航路径等操作。

 

一、将MKMapView控件拖动到Main.storyboard视图中,并拖线到ViewController.m创建控件引用,导入MapView相关依赖包,实现代理,初始化地址与经纬度互转管理器:

#import "ViewController.h"
//导入自定义标注图标类
#import "MyAnnotation.h"
//导入地图依赖包
#import <MapKit/MapKit.h>
//导入定位依赖包
#import <CoreLocation/CoreLocation.h>

@interface ViewController () <MKMapViewDelegate>

//MKMapView地图控件
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
//字符串地址与经纬度互转管理器
@property (nonatomic, strong) CLGeocoder *geocoder;

@end

@implementation ViewController

/*
懒加载,初始化地址与经纬度互转管理器
*/
- (CLGeocoder *)geocoder{
    if(_geocoder == nil){
        _geocoder = [[CLGeocoder alloc] init];
    }
    return _geocoder;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加当前类实现代理方法
    self.mapView.delegate = self;
}

...

@end

二、自定义标注图标:

创建MyAnnotation类,实现MKAnnotation协议:

//  自定义标注图标
//  MyAnnotation.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MyAnnotation : NSObject <MKAnnotation>

//经纬度值
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
//标题
@property (nonatomic, copy) NSString *title;
//二级标题
@property (nonatomic, copy) NSString *subtitle;

@end

三、将起点与终点标注图标,在中间画上导航路径:

1.创建导航路径:

/*
 自定义方法:创建导航路径
 */
- (void)createPath{
    NSString *fromA = @"上海";
    NSString *toA = @"杭州";
    
    //1.将起点地址转为经纬度
    [self.geocoder geocodeAddressString:fromA completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if(error){//地址有错误则返回
            return;
        }
        //得到起点
        CLPlacemark *fromCLP = [placemarks firstObject];
        
        //2.将终点地址转为经纬度
        [self.geocoder geocodeAddressString:toA completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            if(error){//地址有错误则返回
                return;
            }
            //得到终点
            CLPlacemark *toCLP = [placemarks firstObject];
            
            //3.添加线路到地图上
            [self addLineToMap:fromCLP toCLP:toCLP];
        }];
    }];
}

2.将线路添加到地图上:

/*
 自定义方法:将线路添加到地图上
 */
- (void)addLineToMap:(CLPlacemark *)fromCLP toCLP:(CLPlacemark *)toCLP{
    //1.创建方向请求
    MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
    
    //2.设置起点
    MKPlacemark *fromMKP = [[MKPlacemark alloc] initWithPlacemark:fromCLP];
    request.source = [[MKMapItem alloc] initWithPlacemark:fromMKP];
    //标注起点
    [self addAnnotatio:fromMKP];
    
    //3.设置终点
    MKPlacemark *toMKP = [[MKPlacemark alloc] initWithPlacemark:toCLP];
    request.destination = [[MKMapItem alloc] initWithPlacemark:toMKP];
    //标注终点
    [self addAnnotatio:toMKP];
    
    //4.创建方向对象
    MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
    
    //5.计算所有路线
    [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
        NSLog(@"路线条数:%d", response.routes.count);
        
        //获取所有路线
        for (MKRoute *route in response.routes) {
            //添加线路到MapView中
            [self.mapView addOverlay:route.polyline];
        }
        
    }];
}

3.创建自定义标注图标,并加入地图中:

/*
 自定义方法:创建自定义标注图标,并加入地图中
 */
- (void)addAnnotatio:(MKPlacemark *)mkp{
    //创建自定义标注图标
    MyAnnotation *annotation = [[MyAnnotation alloc] init];
    //设置经纬度
    annotation.coordinate = mkp.location.coordinate;
    //设置标题
    annotation.title = mkp.name;
    //将标注图标添加到地图上
    [self.mapView addAnnotation:annotation];
    
}

4.重写mapView-rendererForOverlay方法,将线画到地图上:

/*
 MKMapView方法:必须重写,将线画到地图上
 */
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay{
    //将线画到地图上
    MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
    //设置线的颜色
    renderer.strokeColor = [UIColor blueColor];
    return renderer;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值