Using the system's Map app to navigate, only need to pass in the start and end point, start parameters, call Mkmapitem class method Openmapwithitems:launchoptions: To implement positioning, call this method will open the system's Map app.
Let's take a look at this method:
+ (BOOL) Openmapswithitems: (Nsarray *) Mapitems launchoptions: (nsdictionary *) launchoptions;
① The first parameter is an Mapitem array, each Mapitem can contain a landmark, with two Mapitem, one as the starting point and one for the end.
You can use Mkplacemark initialization, and Mkplacemark can use Clplacemark initialization, to get Clplacemark, you can use Clgeocoder objects to geocode geographical names.
② The second parameter is a dictionary, the keys and values in the dictionary can be found in the manual, values can be used to set the value of the map properties before, note that non-objects need to be wrapped.
The following code shows a place name with two TextField input start and end points, and opens the system's own map app for positioning by clicking the Start Positioning button.
Note that block is a callback method, in order to ensure the order to obtain the destination point of origin, to write a second block code within the first block .
#import "ViewController.h" #import <MapKit/MapKit.h> @interface Viewcontroller () @property (weak, nonatomic) Iboutlet Uitextfield *startview; @property (weak, nonatomic) Iboutlet Uitextfield *endview; @property (Strong, Nonatomic) Clgeocoder *gcoder; @property (Strong, nonatomic) Clplacemark *startmrk,*endmrk; @end @implementation ViewController-( void) Viewdidload {[Super viewdidload]; _gcoder = [[Clgeocoder alloc] init];} -(Ibaction) Startgeo: (ID) Sender {nsstring *startstr = _startview.text; NSString *endstr = _endview.text; [_gcoder geocodeaddressstring:startstr completionhandler:^ (Nsarray *placemarks, Nserror *error) {if (!placemarks.count) return; _STARTMRK = [Placemarks firstobject]; [_gcoder geocodeaddressstring:endstr completionhandler:^ (Nsarray *placemarks, Nserror *error) {if (!placemarks.count) return; _ENDMRK = [Placemarks firstobject]; [Self startnavigation]; }]; }]; }-(void) STArtnavigation{Mkplacemark *startmrk = [[Mkplacemark alloc] initwithplacemark:_startmrk]; Mkplacemark *ENDMRK = [[Mkplacemark alloc] initwithplacemark:_endmrk]; Mkmapitem *startitem = [[Mkmapitem alloc] initwithplacemark:startmrk]; Mkmapitem *enditem = [[Mkmapitem alloc] initwithplacemark:endmrk]; Nsarray *items = @[startitem,enditem]; Nsmutabledictionary *options = [Nsmutabledictionary dictionary]; /* Key NSString * Const Mklaunchoptionsdirectionsmodekey for startup parameters; NSString * Const Mklaunchoptionsmaptypekey; NSString * Const Mklaunchoptionsmapcenterkey; NSString * Const Mklaunchoptionsmapspankey; NSString * Const Mklaunchoptionsshowstraffickey; NSString * Const MKLAUNCHOPTIONSCAMERAKEY General key value is also a constant string, some of which are required by the structure of @ packaging */Options[mklaunchoptionsdirectionsmodekey] = mklaunchoptionsdirectionsmodedriving; Options[mklaunchoptionsmaptypekey] = @ (Mkmaptypestandard); [Mkmapitem Openmapswithitems:items Launchoptions:oPtions]; } @end
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
(81) Use the system to bring your own app to navigate