Navigation-mapkit-Get directions information draw a navigation route

Source: Internet
Author: User

#import "PPViewController.h"

#import <MapKit/MapKit.h>

#import "PPAnnotation.h"

@interface Ppviewcontroller () <MKMapViewDelegate>

/**

* Coded Object

*/

@property (nonatomic, strong) Clgeocoder *geocoder;

@property (Weak, nonatomic) Mkmapview *mapview;

@end

@implementation Ppviewcontroller

Lazy Loading

-(Clgeocoder *) geocoder{

if (!_geocoder) {

_geocoder = [[Clgeocoder alloc] init];

}

return _geocoder;

}

-(void) Drawroute: (ID) Sender {

2. Geocoding with Geo objects to get to landmark objects (Clplacemark)

[Self.geocoder geocodeAddressString:self.startStr completionhandler:^ (Nsarray *placemarks, Nserror *error) {

if (Placemarks.count = = 0) return;

Start Location Landmark

Clplacemark *startplacemark = [Placemarks firstobject];

[Self.geocoder geocodeAddressString:self.endStr completionhandler:^ (Nsarray *placemarks, Nserror *error) {

if (Placemarks.count = = 0) return;

Add two pins-end, start

Ppannotation *startanno = [[Ppannotation alloc] init];

Startanno.title = startplacemark.locality;

Startanno.subtitle = Startplacemark.name;

Startanno.coordinate = startPlacemark.location.coordinate;

[Self.mapview Addannotation:startanno];

End Location Landmark

Clplacemark *endplacemark = [Placemarks firstobject];

Ppannotation *endanno = [[Ppannotation alloc] init];

Endanno.title = endplacemark.locality;

Endanno.subtitle = Endplacemark.name;

Endanno.coordinate = endPlacemark.location.coordinate;

[Self.mapview Addannotation:endanno];

3. Re-use the acquired landmark object (Clplacemark) to create (Mkpalcemark)-Start of item

[Self Startdirectionswithstartclplacemark:startplacemark endclplacemark:endplacemark];

}];

}];

}

-(void) Viewdidload {

[Super Viewdidload];

Do any additional setup after loading the view.

Mkmapview *mapview = [[Mkmapview alloc] Initwithframe:[uiscreen mainscreen].bounds];

Self.mapview = Mapview;

[Self.view AddSubview:self.mapView];

Set up Proxy

Self.mapView.delegate = self;

Setup mode

Self.mapView.mapType = Mkmaptypestandard;

Set up tracking

Self.mapView.userTrackingMode = Mkusertrackingmodefollow;

Set Xuanzhuan

self.mapView.rotateEnabled = NO;

//

UIButton *button = [UIButton buttonwithtype:uibuttontypecontactadd];

Button.frame = CGRectMake (100, 100, 100, 100);

[Self.view Addsubview:button];

[Button addtarget:self action: @selector (Drawroute:) forcontrolevents:uicontroleventtouchupinside];

}

/**

* Send request, get route information

*

* @param startclplacemark Start landmark

* @param Endclplacemark Terminal landmark

*/

-(void) Startdirectionswithstartclplacemark: (Clplacemark *) Startclplacemark Endclplacemark: (CLPlacemark *) Endclplacemark

{

0. Create Start and End objects

Mkplacemark *startmkplacemark = [[Mkplacemark alloc] initwithplacemark:startclplacemark];

Mkmapitem *startitem = [[Mkmapitem alloc] initwithplacemark:startmkplacemark];

Mkplacemark *endmkplacemark = [[Mkplacemark alloc] initwithplacemark:endclplacemark];

Mkmapitem *enditem = [[Mkmapitem alloc] initwithplacemark:endmkplacemark];

1. Send request to Apple server get navigation route

Create a Request object that describes the start-end point

Mkdirectionsrequest *request = [[Mkdirectionsrequest alloc] init];

Request.source = StartItem;

Request.destination = Enditem;

Mkdirections *directions = [[Mkdirections alloc] initwithrequest:request];

2. Draw a navigation route based on the route information returned by the server

When the calculation is complete, calling block will return a response (containing the route information)

[Directions calculatedirectionswithcompletionhandler:^ (Mkdirectionsresponse *response, NSError *error) {

2.1 Printing the returned route information

For (Mkroute *route in response.routes) {

Logred (@ "%f-%f", route.distance/1000, route.expectedtraveltime/3600);

2.2 Draw a route-add a cover to the map

The geometry of the current route is obscured to the map, and the map automatically draws the route according to the mask

The line width and color are queried when the system starts to draw the route

[Self.mapview AddOverlay:route.polyline];

Nsarray *array = route.steps;

For (Mkroutestep *step in array) {

Loggreen (@ "%@-%f", step.instructions, step.distance);

}

}

}];

}

#pragma mark-mkmapviewdelegate

-(void) Mapview: (Mkmapview *) Mapview didupdateuserlocation: (mkuserlocation *) userlocation

{

Move the map to the current user's location

[Self.mapview setCenterCoordinate:userLocation.location.coordinate Animated:yes];

}

/**

* Called when a route is drawn (called when a mask is added)

*

* @param mapview Mapview

* @param Overlay

*/

-(Mkoverlayrenderer *) Mapview: (Mkmapview *) Mapview Rendererforoverlay: (id<mkoverlay>) Overlay

{

Create a path to cover

Mkpolylinerenderer*line = [[Mkpolylinerenderer alloc] initwithpolyline:overlay];

Line.linewidth = 10; Course width

Line.strokecolor = [Uicolor redcolor];//route width

return line;

}

Navigation-mapkit-Get directions information draw a navigation route

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.