说明:
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;
}