iOS system navigation/self-drawing navigation routes

Source: Internet
Author: User
Tags polyline

System comes with navigation

/** System comes with navigation current location navigate to destination 1. GeoCode by Destination 2. Encapsulates the current location and destination as a Mkmapitem object 3. Navigate using the Mkmapitem Openmapswithitems:launchoptions: Method */@interfaceViewcontroller ()//input box for destination@property (Weak, nonatomic) Iboutlet Uitextfield *Destinationfield;/** * Click on the button to start navigation*/-(ibaction) navigate;@end@implementationViewcontroller-(ibaction) Navigate {//1. Get the destination entered by the userNSString *destination =Self.destinationField.text; if(Destination.length = =0) {        return; }        //2. GeocodingClgeocoder *geocoder =[[Clgeocoder alloc] init]; [Geocoder geocodeaddressstring:destination Completionhandler:^ (Nsarray *placemarks, Nserror *error) {        if(Placemarks.count = =0|| Errorreturn ; //2.1. Take out geo-coded landmarksClplacemark *CLPM =[Placemarks Firstobject]; //2.2. Use Clplacemark to create MkplacemarkMkplacemark *MKPM =[[Mkplacemark alloc] INITWITHPLACEMARK:CLPM]; //2.3. Use Mkplacemark to create a destination MkmapitemMkmapitem *destinationitem =[[Mkmapitem alloc] initwithplacemark:mkpm]; //2.4. Get the Mkmapitem of the starting pointMkmapitem *sourceitem =[Mkmapitem mapitemforcurrentlocation]; //2.5. Start Navigation[self startnavigatewithsourceitem:sourceitem destinationitem:destinationitem]; }];}/** * Start navigation * * @param sourceitem Start Item * @param destinationitem end of item*/- (void) Startnavigatewithsourceitem: (Mkmapitem *) SourceItem Destinationitem: (Mkmapitem *) destinationitem{//1. Place the start and end item into the arrayNsarray *items =@[sourceitem, Destinationitem]; //2. Set options parameters (dictionary)//Mklaunchoptionsdirectionsmodekey: Navigation mode//mklaunchoptionsmaptypekey: Map Type//Mklaunchoptionsshowstraffickey: Whether to show traffic conditionsNsdictionary *options =@{mklaunchoptionsdirectionsmodekey:mklaunchoptionsdirectionsmodedriving, mklaunchoptionsmapt        Ypekey: @ (Mkmaptypehybrid), Mklaunchoptionsshowstraffickey: @YES}; //3. Start Navigation[Mkmapitem openmapswithitems:items launchoptions:options];}@end

Navigating a custom drawing route

#import "ViewController.h"#import<MapKit/MapKit.h>/** Draw route according to destination self-implementation 1. Encapsulates the current location and destination for the Mkmapitem object 2.MKDirectionsRequest object wrapper source address and purpose address 3. Mkdirections Object Calculatedirectionswithcompletionhandler: Method to draw 4. Use Mapview Addoverlay:polyline] to add the traversed line 6. Set Mapview agent, comply with protocol, and implement-(Mkoverlayrenderer *) Mapview: (Mkmapview *) Mapview rendererforoverlay: (Mkpolyline *) Overlay Method ( This method is executed when a mask is added to the Mapview to the map, and the drawing line is displayed correctly .*/@interfaceViewcontroller () <MKMapViewDelegate>//view of the map@property (Weak, nonatomic) Iboutlet Mkmapview *Mapview;//input box for destination@property (Weak, nonatomic) Iboutlet Uitextfield *Destinationfield;/** * Start drawing lines after clicking*/-(ibaction) drawLine;@end@implementationViewcontroller- (void) viewdidload{[Super Viewdidload]; Self.mapview.Delegate=Self ;}-(ibaction) drawLine {//0. Exit the keyboard[Self.view Endediting:yes]; //1. Get the destination entered by the userNSString *destination =Self.destinationField.text; if(Destination.length = =0) {        return; }        //2. GeocodingClgeocoder *geocoder =[[Clgeocoder alloc] init]; [Geocoder geocodeaddressstring:destination Completionhandler:^ (Nsarray *placemarks, Nserror *error) {        if(Placemarks.count = =0|| Errorreturn; //2.1. Get the Clplacemark objectClplacemark *CLPM =[Placemarks Firstobject]; //2.2. Use Clplacemark to create MkplacemarkMkplacemark *MKPM =[[Mkplacemark alloc] INITWITHPLACEMARK:CLPM]; //2.3. Create a Mkmapitem object for the destinationMkmapitem *destinationitem =[[Mkmapitem alloc] initwithplacemark:mkpm]; //2.4. Mkmapitem of the starting pointMkmapitem *sourceitem =[Mkmapitem mapitemforcurrentlocation]; //2.5. Start drawing lines[self drawlinewithsourceitem:sourceitem destinationitem:destinationitem]; }];}/** * Start Drawing line * * @param sourceitem Start Item * @param destinationitem end of item*/- (void) Drawlinewithsourceitem: (Mkmapitem *) SourceItem Destinationitem: (Mkmapitem *) destinationitem{//1. Create a Mkdirectionsrequest objectMkdirectionsrequest *request =[[Mkdirectionsrequest alloc] init]; //1.1. Set the start itemRequest.source =SourceItem; //1.2. Set the end itemRequest.destination =Destinationitem; //2. Create a Mkdirections objectMkdirections *directions =[[Mkdirections alloc] initwithrequest:request]; //3. Request/Calculate (the method will come when the route information is requested)[Directions calculatedirectionswithcompletionhandler:^ (Mkdirectionsresponse *response, NSError *error) {        //3.1. When there is an error, or the number of routes is 0 direct return        if(Error | | response.routes.count = =0)return; NSLog (@"%ld", Response.routes.count); //3.2. Traverse all the routes         for(Mkroute *routeinchresponse.routes) {//3.3. Remove route (follow Mkoverlay)Mkpolyline *polyline =Route.polyline; //3.4. Add a route to the map[Self.mapview Addoverlay:polyline]; }    }];}/** * When a mask is added to the map, the method is executed * * @param overlay Obey Mkoverlay * * * @return Draw line Rendering*/-(Mkoverlayrenderer *) Mapview: (Mkmapview *) Mapview rendererforoverlay: (Mkpolyline *) overlay{Mkpolylinerenderer*poly =[[Mkpolylinerenderer alloc] initwithpolyline:overlay]; Poly.strokecolor=[Uicolor Yellowcolor]; Poly.linewidth=5.0; returnPoly;}@end

iOS system navigation/self-drawing navigation routes

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.