Mapkit Map Dash

Source: Internet
Author: User

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

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.