User authorization is needed as long as it is used to get the user's location
#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface Viewcontroller () <mkmapviewdelegate>
@property(nonatomic,strong)Uitextfield*destination;
@property(nonatomic,strong)Mkmapview*mapview;
@property(nonatomic,strong)Cllocationmanager *mgr;
@end
@implementation Viewcontroller
-(void) viewdidload {
[Super viewdidload];
self. Mgr=[[cllocationmanager alloc]init];
[self. Mgr requestalwaysauthorization];
mkmapview *map=[[mkmapview alloc]init];
Map. frame=self. View. Bounds;
self. Mapview=map;
[self. View Addsubview: map];
self. Mapview. Delegate=self;
Uitextfield *field=[[uitextfield alloc]init];
Field. frame=CGRectMake(ten, +, + );
self. Destination=field;
Field. text=@ " Xian ";
[self. View addsubview: field];
[self drawLine];
}
Dash
-(void) DrawLine
{
[self. View endediting:YES];
clgeocoder *geo=[[clgeocoder alloc]init];
[Geo geocodeaddressstring:self. Destination. Text Completionhandler: ^ (nsarray *placemarks, nserror *error) {
if (placemarks. Count= =0| | Error) {
return ;
}
// get The destination item and current location
clplacemark *pm=[placemarks firstobject];
mkplacemark *mkp=[[mkplacemark alloc]initwithplacemark:p m];
mkmapitem *destinationitem=[[mkmapitem alloc]initwithplacemark: MKP];
mkmapitem *useritem=[mkmapitem mapitemforcurrentlocation];
//1 Creating route requests
mkdirectionsrequest*request=[[mkdirectionsrequest alloc]init];
// Set start point end
Request. source=useritem;
Request. destination=destinationitem;
// Create route manager
mkdirections *direction=[[mkdirections alloc]initwithrequest: request];
// dash
[Direction Calculatedirectionswithcompletionhandler: ^ (mkdirectionsresponse *response, Nserror *error) {
for (mkroute*route in response. Routes) {
// Get the line
mkpolyline *line=route. Polyline;
// Add to map
[self. Mapview addoverlay: line];
}
}];
}];
}
-(mkoverlayrenderer*) Mapview: (mkmapview *) Mapview Rendererforoverlay: (ID< Mkoverlay>) Overlay
{
// Create renderer
mkpolylinerenderer *render=[[mkpolylinerenderer alloc]initwithoverlay: overlay] ;
// Set the color of the segment
Render. strokecolor=[uicolor redcolor];
// Set line width
Render. linewidth=5;
return render;
}
@end
Mapkit Map Dash