导航

#import"ViewController.h"

#import MapKit/MapKit.h

#import CoreLocation/CoreLocation.h

@interfaceViewController()

/**地理编码*/

@property(nonatomic,strong)CLGeocoder*geoC;

@end

@implementationViewController

#pragma mark -懒加载

-(CLGeocoder*)geoC

{

if(!_geoC) {

_geoC= [[CLGeocoderalloc]init];

}

return_geoC;

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event

{

[self.geoCgeocodeAddressString:@"广州"completionHandler:^(NSArray *_Nullableplacemarks,NSError*_Nullableerror) {

//广州地标

CLPlacemark*gzPL = [placemarksfirstObject];

[self.geoCgeocodeAddressString:@"上海"completionHandler:^(NSArray *_Nullableplacemarks,NSError*_Nullableerror) {

//上海地标

CLPlacemark*shPL = [placemarksfirstObject];

[selfsystemNavWithBeginPL:gzPLendPL:shPL];

}];

}];

}

/**

*根据起点和终点地标对象,调用系统APP进行导航

*

*@param beginCLPL起点地标

*@param endCLPL终点地标

*/

-(void)systemNavWithBeginPL:(CLPlacemark*)beginCLPL endPL : (CLPlacemark*)endCLPL

{

//调用系统的APP进行导航

//地图起点地标对象

MKPlacemark*beginPL = [[MKPlacemarkalloc]initWithPlacemark:beginCLPL];

//起点

MKMapItem*beginItem = [[MKMapItemalloc]initWithPlacemark:beginPL];

//地图终点地标对象

MKPlacemark*endPL = [[MKPlacemarkalloc]initWithPlacemark:endCLPL];

//终点

MKMapItem*endItem = [[MKMapItemalloc]initWithPlacemark:endPL];

//起点和终点数组

NSArray*items =@[beginItem, endItem];

//设置地图启动项(导航模式:驾驶,地图类型:混合,是否显示交通:是)

NSDictionary*dic =@{

MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,

MKLaunchOptionsMapTypeKey:@(MKMapTypeHybrid),

MKLaunchOptionsShowsTrafficKey:@(YES),

};

//给定两个点,起点和终点,然后设置启动项,开始调用系统APP进行导航

[MKMapItemopenMapsWithItems:itemslaunchOptions:dic];

}

你可能感兴趣的:(导航)