简单界面搭建:
示例代码:
#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController ()
// 目标地址
@property (weak, nonatomic) IBOutlet UITextField *destination_TF;
@end
@implementation ViewController
// 开始导航按钮点击事件
- (IBAction)navgationBtnClick:(UIButton *)sender {
// 将起点和重点传递给系统地图
// 1.获取起点和终点
// 起点: 当前定位点
MKMapItem *sourceItem = [MKMapItem mapItemForCurrentLocation];
// 终点: 手动输入的地址 地理编码
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
[geocoder geocodeAddressString:self.destination_TF.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (placemarks.count == 0 || error) {
return ;
}
CLPlacemark *clPlacemark = placemarks.lastObject;
MKPlacemark *mkPlacemark = [[MKPlacemark alloc]initWithPlacemark:clPlacemark];
MKMapItem *destinItem = [[MKMapItem alloc]initWithPlacemark:mkPlacemark];
// 2.打开地图
[MKMapItem openMapsWithItems:@[sourceItem,destinItem] launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsMapTypeKey:@0}];
}];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
打开地图:
显示导航:
MKMapItem 地图项目属性简介:
//地标对象
@property (nonatomic, readonly) MKPlacemark *placemark;
//地图项目是否在定位点上
@property (nonatomic, readonly) BOOL isCurrentLocation;
//商业名称
@property (nonatomic, copy, nullable) NSString *name;
//电话
@property (nonatomic, copy, nullable) NSString *phoneNumber;
//主页
@property (nonatomic, strong, nullable) NSURL *url;
//时区
@property (nonatomic, copy, nullable) NSTimeZone *timeZone NS_AVAILABLE(10_11, 9_0);
//初始化方法
+ (MKMapItem *)mapItemForCurrentLocation; //把定位点转化为地图项目
- (instancetype)initWithPlacemark:(MKPlacemark *)placemark; //根据地标对象进行初始化
- (BOOL)openInMapsWithLaunchOptions:(nullable NSDictionary<NSString *, id> *)launchOptions __TVOS_PROHIBITED; //打开系统地图,并且在地图上展示当前的地图项目
+ (BOOL)openMapsWithItems:(NSArray<MKMapItem *> *)mapItems launchOptions:(nullable NSDictionary<NSString *, id> *)launchOptions __TVOS_PROHIBITED; //打开系统地图,并且在传入的地图项目间进行路线导航
@end
MK_EXTERN NSString * const MKLaunchOptionsDirectionsModeKey NS_AVAILABLE(10_9, 6_0) __TVOS_PROHIBITED; // 导航模式
MK_EXTERN NSString * const MKLaunchOptionsMapTypeKey NS_AVAILABLE(10_9, 6_0) __TVOS_PROHIBITED __WATCHOS_PROHIBITED; // 地图类型
MK_EXTERN NSString * const MKLaunchOptionsShowsTrafficKey NS_AVAILABLE(10_9, 6_0) __TVOS_PROHIBITED __WATCHOS_PROHIBITED; // 是否显示交通情况
// 导航模式
MK_EXTERN NSString * const MKLaunchOptionsDirectionsModeDriving NS_AVAILABLE(10_9, 6_0) __TVOS_PROHIBITED; 驾车
MK_EXTERN NSString * const MKLaunchOptionsDirectionsModeWalking NS_AVAILABLE(10_9, 6_0) __TVOS_PROHIBITED; 步行
MK_EXTERN NSString * const MKLaunchOptionsDirectionsModeTransit NS_AVAILABLE(10_11, 9_0) __TVOS_PROHIBITED; 公共交通
// If center and span are present, having a camera as well is undefined
MK_EXTERN NSString * const MKLaunchOptionsMapCenterKey NS_AVAILABLE(10_9, 6_0) __TVOS_PROHIBITED; // 地图中心点
MK_EXTERN NSString * const MKLaunchOptionsMapSpanKey NS_AVAILABLE(10_9, 6_0) __TVOS_PROHIBITED; // 地图跨度
MK_EXTERN NSString * const MKLaunchOptionsCameraKey NS_AVAILABLE(10_10, 7_1) __TVOS_PROHIBITED; // 航拍角度