(81) Use the system to bring your own app to navigate

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.