作品链接:http://www.jianshu.com/users/1e0f5e6f73f6/top_articles
1.导入<MapKit/MapKit.h>
2.懒加载
@property (nonatomic, strong) CLGeocoder *geoC;
- (CLGeocoder *)geoC
{
if (!_geoC) {
_geoC = [[CLGeocoder alloc] init];
}
return _geoC;
}
3.将起点和终点, 传递给系统地图
- (void)beginNavWithBpl:(CLPlacemark *)beginP andEndP:(CLPlacemark *)endP
{
//创建开始的地图项
CLPlacemark *clpB = beginP;
MKPlacemark *mkPB = [[MKPlacemark alloc] initWithPlacemark:clpB];
MKMapItem *beginI = [[MKMapItem alloc] initWithPlacemark:mkPB];
//创建结束的地图项
CLPlacemark *clp = endP;
MKPlacemark *mkP = [[MKPlacemark alloc] initWithPlacemark:clp];
MKMapItem *endI = [[MKMapItem alloc] initWithPlacemark:mkP];
//地图项数组
NSArray *items = @[beginI,endI];
//启动字典
NSDictionary *dict = @{
//导航方式
MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeKey,
//地图类型
MKLaunchOptionsMapTypeKey:@(MKMapTypeHybrid),
//是否显示交通
MKLaunchOptionsShowsTrafficKey:@(YES)
};
[MKMapItem openMapsWithItems:items launchOptions:dict];
}
4.利用系统APP, 进行导航
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.geoC geocodeAddressString:@"昆明" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
// 昆明地标
CLPlacemark *kmP = [placemarks firstObject];
[self.geoC geocodeAddressString:@"郑州" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
// 郑州地标
CLPlacemark *zzP = [placemarks firstObject];
[self beginNavWithBpl:kmP andEndP:zzP];
}];
}];
}
网友评论