#import "ViewController.h" #import <MapKit/MapKit.h> #import <CoreLocation/CoreLocation.h> @interface Viewcontroller ()/** geocoding */@property (nonatomic, strong) Clgeocoder *GEOC; @end @implementation Viewcontroller#pragma Mark-Lazy Loading-(Clgeocoder *) geoc{if (!_GEOC) {_GEOC = [[Clgeocoder alloc] init]; } return _GEOC;} -(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *) event{[SELF.GEOC geocodeaddressstring:@] Guangzhou "completionhandler:^ (Nsarray<clplacemark *> * _nullable placemarks, Nserror * _nullable error) {//Guangzhou landmark Clplacemark *GZPL = [Placemarks firstobject]; [SELF.GEOC geocodeaddressstring:@ "Shanghai" completionhandler:^ (Nsarray<clplacemark *> * _Nullable Placemarks, Nserror * _nullable error) {//Shanghai landmark Clplacemark *SHPL = [Placemarks firstobject]; [Self systemnavwithbeginpl:gzpl ENDPL:SHPL]; }]; }]; }/** * Based on the origin and destination landmarksLike, call the system app to navigate * * @param BEGINCLPL starting point landmark * @param ENDCLPL endpoint landmark */-(void) SYSTEMNAVWITHBEGINPL: (Clplacemark *) BEGINCLPL E NDPL: (Clplacemark *) endclpl{//Call system app to navigate//Map Origin Landmark object Mkplacemark *BEGINPL = [[Mkplacemark alloc] Initwithpl ACEMARK:BEGINCLPL]; Starting point Mkmapitem *beginitem = [[Mkmapitem alloc] INITWITHPLACEMARK:BEGINPL]; Map end Landmark object Mkplacemark *ENDPL = [[Mkplacemark alloc] INITWITHPLACEMARK:ENDCLPL]; End Mkmapitem *enditem = [[Mkmapitem alloc] INITWITHPLACEMARK:ENDPL]; Starting and ending arrays nsarray *items = @[beginitem, Enditem]; Set map Startup items (Navigation mode: driving, Map type: mixed, whether to show traffic: YES) nsdictionary *dic = @{Mklaunchoptionsdirectionsmodek Ey:mklaunchoptionsdirectionsmodedriving, Mklaunchoptionsmaptypekey: @ (Mkmaptypehybrid), Mklaunchoptionsshowstraffickey: @ (YES),}; Given two points, starting point and end point, then set the startup item and start calling the system app to navigate [MkmaPitem openmapswithitems:items launchoptions:dic];} @end
Plotting System map routes