iOS native map development advanced-use navigation and nearby points of interest to retrieve
Source: Internet
Author: User
<span id="Label3"></p>iOS native map development advanced-use navigation and nearby points of interest to retrieve<p><p>The Mapkit framework in iOS is a great support for Internationalization. In the previous blogs, the basic usage of this map frame and the addition of annotations and overlays are described in detail, and this blog will cover the development of two more useful features: line navigation and POI Search. Links to the previous blogs are as Follows:</p></p><p><p>The basic usage of map is detailed: http://my.oschina.net/u/2340880/blog/415360.</p></p><p><p>Add a PIN to your custom callout: http://my.oschina.net/u/2340880/blog/415441.</p></p><p><p>Add Map overlay: http://my.oschina.net/u/2340880/blog/415611.</p></p>first, Line navigation 1, from the relationship between several classes<p><p>(1) Mkplacemark</p></p><p><p>A location information class, as Follows:</p></p><pre class="brush:cpp;toolbar: true; auto-links: false;"><pre class="brush:cpp;toolbar: true; auto-links: false;">@interface Mkplacemark:clplacemark//initialization method, by given a latitude and longitude and location information Dictionary-(instancetype) initwithcoordinate: ( Cllocationcoordinate2d) coordinate addressdictionary: (nsdictionary *) addressdictionary;//country code @property (non atomic, readonly) nsstring *countrycode; @end</pre></pre><p><p>(2) Mkmapitem</p></p><p><p>A location node class that contains many of the location information for this node, as Follows:</p></p><pre class="brush:cpp;toolbar: true; auto-links: false;"><pre class="brush:cpp;toolbar: true; auto-links: false;">@interface mkmapitem:nsobject//the location Information object of the current node @property (nonatomic, readonly) Mkplacemark *placemark;//is the current location @property (nonatomic, readonly) BOOL iscurrentlocation;//node name @property (nonatomic, copy) nsstring *name;//phone number @property (nonatomic, copy) NSString * Phonenumber;//url @property (nonatomic, Strong) nsurl *url;//create The current location as a node + (mkmapitem *) mapitemforcurrentlocation;// Create a node from a location Information-(instancetype) initwithplacemark: (mkplacemark *) placemark; @end</pre></pre><p><p>(3) Mkdirectionsrequest</p></p><p><p>Navigation Request Class</p></p><pre class="brush:cpp;toolbar: true; auto-links: false;"><pre class="brush:cpp;toolbar: true; auto-links: false;">@interface Mkdirectionsrequest:nsobject//start node-(mkmapitem *) source ns_available (10_9, 6_0);-(void) setsource: ( Mkmapitem *) source ns_available (10_9, 7_0);//destination node-(mkmapitem *) destination ns_available (10_9, 6_0);-(void) Setdestination: (mkmapitem *) Destination ns_available (10_9, 7_0); @end</pre></pre><p><p>This class also has some extended settings properties:</p></p><p><p></p></p><p><p>@property (nonatomic) Mkdirectionstransporttype transporttype;</p></p><p><p>Set the route retrieval type, enumerated as Follows:</p></p><pre class="brush:cpp;toolbar: true; auto-links: false;"><pre class="brush:cpp;toolbar: true; auto-links: false;">typedef ns_options (nsuinteger, Mkdirectionstransporttype) {mkdirectionstransporttypeautomobile = 1 << 0,//fit navigating while driving mkdirectionstransporttypewalking = 1 << 1,//for walking navigation mkdirectionstransporttypeany = 0x0 Fffffff//any situation};</pre></pre><p><p></p></p><p><p>@property (nonatomic) BOOL requestsalternateroutes;</p></p><p><p>Set whether to search multiple lines</p></p><p><p></p></p><p><p>@property (nonatomic, copy) nsdate *departuredate;</p></p><p><p>Set departure date</p></p><p><p></p></p><p><p>@property (nonatomic, copy) nsdate *arrivaldate;</p></p><p><p>Set Arrival Date</p></p><p><p></p></p><p><p>(4) mkdirections</p></p><p><p>Connection classes to get data from Apple server</p></p><pre class="brush:cpp;toolbar: true; auto-links: false;"><pre class="brush:cpp;toolbar: true; auto-links: false;">@interface mkdirections:nsobject//initialization Method-(instancetype) initwithrequest: (mkdirectionsrequest *) Request Ns_ Designated_initializer;//start Computing Line Information-(void) calculatedirectionswithcompletionhandler: (mkdirectionshandler) Completionhandler;//begins to calculate time Information-(void) calculateetawithcompletionhandler: (mketahandler) completionhandler;//cancel-( Void) cancel;//is Calculating @property (nonatomic, readonly, Getter=iscalculating) BOOL calculating; @end</pre></pre><p><p>(5) Mkdirectionsresponse</p></p><p><p>Line information Result class</p></p><pre class="brush:cpp;toolbar: true; auto-links: false;"><pre class="brush:cpp;toolbar: true; auto-links: false;">@interface mkdirectionsresponse: [email protected] (nonatomic, readonly) mkmapitem *source;//start @property (nonatomic, Readonly) mkmapitem *destination;//endpoint @property (nonatomic, readonly) nsarray *routes; Line Planning Array @end</pre></pre><p><p>(6) Mketresponse</p></p><p><p>Time Information Result class</p></p><pre class="brush:cpp;toolbar: true; auto-links: false;"><pre class="brush:cpp;toolbar: true; auto-links: false;">@interface mketaresponse: [email protected] (nonatomic, readonly) mkmapitem *source;//beginning @property (nonatomic, readonly) Mkmapitem *destination;//end @property (nonatomic, readonly) nstimeinterval expectedtraveltime;//time consuming @end</pre></pre><p><p>(7) Mkroute</p></p><p><p>Line information class, the navigation line result is this type of object</p></p><pre class="brush:cpp;toolbar: true; auto-links: false;"><pre class="brush:cpp;toolbar: true; auto-links: false;">@interface mkroute: [email protected] (nonatomic, readonly) nsstring *name; Line name @property (nonatomic, readonly) Nsarray *advisorynotices; Precautions @property (nonatomic, readonly) cllocationdistance distance; Distance @property (nonatomic, readonly) nstimeinterval expectedtraveltime;//time-consuming @property (nonatomic, readonly) Mkdirectionstransporttype transporttype; The retrieved type @property (nonatomic, readonly) mkpolyline *polyline; Line Coverings @property (nonatomic, readonly) nsarray *steps; Line details Array @end</pre></pre><p><p>(8) Mkroutestep</p></p><p><p>Line details class, The information for each step in the line is the object of this class</p></p><pre class="brush:cpp;toolbar: true; auto-links: false;"><pre class="brush:cpp;toolbar: true; auto-links: false;">@interface mkroutestep: [email protected] (nonatomic, readonly) nsstring *instructions; Node Information @property (nonatomic, readonly) nsstring *notice; Precautions @property (nonatomic, readonly) mkpolyline *polyline; Line Coverings @property (nonatomic, readonly) cllocationdistance distance; Distance @property (nonatomic, readonly) mkdirectionstransporttype transporttype; Navigation Type @end</pre></pre><p><p>See so many classes above, you may feel confused, then do not worry, although the class is complicated, but the logic between them is very clear, the following is an example of line Navigation.</p></p>2, the line navigation<pre class="brush:cpp;toolbar: true; auto-links: false;">- (void) viewdidload { [super viewdidload]; // Map Initialization Settings mapView =[[MKMapView alloc]initWithFrame:self.view.frame]; mapview.region=mkcoordinateregionmake (cllocationcoordinate2dmake (39.26, 116.3), Mkcoordinatespanmake (5, 5)); mapview.maptype=mkmaptypestandard; mapView.delegate=self; [self.view addSubview:mapView]; //navigation Settings cllocationcoordinate2d fromcoor= Cllocationcoordinate2dmake (39.26, 116.3); cllocationcoordinate2d tocoor = cllocationcoordinate2dmake (33.33, 113.33); //create starting point and destination information MKPlacemark *fromPlace = [[MKPlacemark alloc] initWithCoordinate:fromcoor addressDictionary:nil]; MKPlacemark *toPlace = [[MKPlacemark alloc]initWithCoordinate:tocoor Addressdictionary:nil]; //creating a departure node and a destination node MKMapItem * Fromitem = [[mkmapitem alloc]initwithplacemark:fromplace]; mkmapitem * toitem = [[mkmapitem alloc]initwithplacemark:toplace]; // Initialize navigation search Request MKDirectionsRequest *request = [[MKDirectionsRequest Alloc]init]; request.source=fromitem; request.destination=toitem ; request.requeStsalternateroutes=yes; //initialization Request Retrieval mkdirections *directions = [[mkdirections alloc]initwithrequest:request]; //starts the search and the results are returned in block [directions calculatedirectionswithcompletionhandler:^ (MKDirectionsResponse *response, nserror *error) { if (error) { nslog (@ "error:%@", error); }else{ //a line mkroute in the results of the navigation line extraction *route =response.routes[0]; Extract the details of each step in the route NSArray * Steparray = [nsarray arraywitharray:route.steps]; //to Traverse for (int i=0; i<steparray.count; i++) { Details of the line node mkroutestep * step = " stepArray[i]; //add a pin at this node mkpointannotation * point = [[m<a "href=" http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001& Ch=0&di=128&fv=19&is_app=0&jk=540947cac5de81c0&k=%c4%bf%b5%c4%b5%d8&k0=%c4%bf%b5%c4%b5 %d8&kdi0=0&lUki=1&mcpm=0&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid= c081dec5ca470954&ssp2=1&stid=9&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3a%2f% 2fwww%2eth7%2ecn%2fprogram%2fios%2f201505%2f453699%2eshtml&urlid=0 "target=" _blank ">KPointAnnotation alloc]init]; point.coordinate=step.polyline.coordinate; point.title=step.instructions; point.subtitle=step.notice; [mapView addAnnotation:point]; //add This segment to the map [mapview addoverlay:step.polyline]; } } }]; }//map Overlay Agent Method-(mkoverlayrenderer *) mapview: (mkmapview *) Mapview rendererforoverlay: (id) overlay{ mkpolylinerenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay]; renderer.strokeColor = [UIColor redColor]; renderer.lineWidth = 4.0; return renderer;} Proxy method for labeling-(mkannotationview *) mapview: (mkmapview *) mapview viewforannotation: (id) annotation{ mkpinannotationview * view= [[mkpinannotationview alloc] Initwithannotation:annotation reuseidentifier:@ "anno"]; view.canshowcallout=yes; return view;}</pre><p><p>The effect is as Follows:</p></p><p><p></p></p>second, nearby points of interest search<p><p>The logic of point-of-interest retrieval is similar to the logic of navigation line retrieval, which is demonstrated directly by code:</p></p><pre class="brush:cpp;toolbar: true; auto-links: false;">Create a location information object, the first parameter is latitude and longitude, the second is the latitude to retrieve the range, in meters, and the third for the longitude range, in meters mkcoordinateregion region = mkcoordinateregionmakewithdistance (tocoor, 5000, 5000); // Initializes a retrieval Request object mklocalsearchrequest * req = [[mklocalsearchrequest alloc]init]; //setting Search Parameters req.region=region; //point of interest keyword [email protected] "hotal"; //initialization search MKLocalSearch * ser = [[MKLocalSearch alloc]initWithRequest:req]; //starts the search, and the results are returned in block [ser startwithcompletionhandler:^ ( Mklocalsearchresponse *response, nserror *error) { //point of interest Node array nsarray * array = [ Nsarray arraywitharray:response.mapitems]; for (int i=0; i<array.count; i++ ) { mkmapitem * item= array[i]; mkpointannotation * point = [[mkpointannotation alloc]init]; point.title=item.name; point.subtitle=item.phonenumber; point.coordinate=item.placemark.coordinate; [mapview addannotation:point]; } }];</pre><p><p>The effect is as Follows:</p></p><p><p></p></p><p><p>Original link: iOS native map development advanced--use navigation and nearby points of interest to retrieve</p></p><p><p>iOS native map development advanced-use navigation and nearby points of interest to retrieve</p></p></span>
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