Using Mapkit for navigation

Source: Internet
Author: User

#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "JRAnnotation.h"

@interface Viewcontroller () <MKMapViewDelegate>
@property (Nonatomic,strong) clgeocoder * coder;

@property (Weak, nonatomic) Iboutlet Mkmapview *mapview;

@property (Nonatomic,strong) mkmapitem * FROMITEM;
@property (Nonatomic,strong) mkmapitem * TOITEM;

@end

@implementation Viewcontroller

-(Clgeocoder *) coder{

if (_coder==nil) {
_coder=[[clgeocoder alloc] init];
}

return _coder;
}


-(void) Viewdidload {
[Super Viewdidload];
self.mapview.delegate=self;


}



Require labeling (PIN) for both places
#warning each time you geocode, you can only make one
-(ibaction) addanotation {

Set Start position
NSString *[email protected] "Yantai";
NSString *[email protected] "Xinjiang";

Set Start Pin
[Self.coder geocodeaddressstring:from completionhandler:^ (Nsarray *placemarks, Nserror *error) {

Clplacemark * Frommark=[placemarks Firstobject];
Do not add a pin if you are not positioned to the current location
if (placemarks.count>0) {

Custom PIN Model
Jrannotation * Fromlocation=[[jrannotation Alloc]init];

Set Where to start
Fromlocation.coordinate=frommark.location.coordinate;

Set Title
Fromlocation.title=frommark.name;
fromlocation.subtitle=frommark.locality;

Add a PIN
[Self.mapview addannotation:fromlocation];
}

Set End Pin
[Self.coder geocodeaddressstring:to completionhandler:^ (Nsarray *placemarks, Nserror *error) {

Clplacemark * Tomark=[placemarks Firstobject];
Do not add a pin if you are not positioned to the current location
if (placemarks.count>0) {

Custom PIN Model
Jrannotation * Tolocation=[[jrannotation Alloc]init];

Set Where to start
Tolocation.coordinate=tomark.location.coordinate;

Set Title
Tolocation.title=tomark.name;
tolocation.subtitle=tomark.locality;

Add a PIN
[Self.mapview addannotation:tolocation];

}

Start Dash
[Self Addlinefrom:frommark andend:tomark];
}];

}];




}

-(void) Addlinefrom: (Clplacemark *) Fromclmark andend: (Clplacemark *) toclmark{

1 Defining a direction request
Mkdirectionsrequest * Request=[[mkdirectionsrequest alloc] init];

1> set the starting coordinates of the request
Mkplacemark * Frommark=[[mkplacemark alloc] initwithplacemark:fromclmark];
Mkmapitem * Fromitem=[[mkmapitem alloc] initwithplacemark:frommark];
Request.source=fromitem;
Self.fromitem=fromitem;

2> set the starting coordinates of the request
Mkplacemark * Tomark=[[mkplacemark alloc] initwithplacemark:toclmark];
Mkmapitem * Toitem=[[mkmapitem alloc] initwithplacemark:tomark];
Request.destination=toitem;
Self.toitem=toitem;

2 Getting directions on request
Mkdirections * Direct=[[mkdirections alloc] initwithrequest:request];

[Direct calculatedirectionswithcompletionhandler:^ (Mkdirectionsresponse *response, Nserror *error) {

Traverse all routes and add cover to the map's corresponding route
For (Mkroute * Rote in response.routes) {
[Self.mapview AddOverlay:rote.polyline];
}

}];


}


#pragma mark-mapview
#pragma mark return pin render view
-(Mkannotationview *) Mapview: (Mkmapview *) Mapview viewforannotation: (id<mkannotation>) annotation{
Static NSString * [email protected] "Jranno";

Mkpinannotationview * mk= (Mkpinannotationview *) [Mapview Dequeuereusableannotationviewwithidentifier:identy];

if (Mk==nil) {
Mk=[[mkpinannotationview alloc] Initwithannotation:annotation Reuseidentifier:identy];

Override proxy method must call this method to display text
Mk.canshowcallout=yes;
}

Mk.pincolor=mkpinannotationcolorgreen;
Mk.animatesdrop=yes;
Mk.image=[uiimage imagenamed:@ "Annotationimage.png"];

UIView *view=[[uiview alloc] Initwithframe:cgrectmake (0, 0, 50, 50)];
View.backgroundcolor=[uicolor Greencolor];
Mk.leftcalloutaccessoryview=view;
Mk.rightcalloutaccessoryview=view;

Return MK;

}


#pragma mark-Set the Dash property
-(Mkoverlayrenderer *) Mapview: (Mkmapview *) Mapview Rendererforoverlay: (ID <MKOverlay>) Overlay {
Mkpolylinerenderer * Render=[[mkpolylinerenderer alloc] initwithoverlay:overlay];
render.linewidth=5;
Render.strokecolor=[uicolor Bluecolor];

return render;


}

Start navigation
-(ibaction) guidaction {

Set the start and end navigation positions
Nsarray * [email Protected][self.fromitem,self.toitem];

Set the walk or drive and whether to show traffic information
Nsdictionary *[email protected]{mklaunchoptionsdirectionsmodekey:mklaunchoptionsdirectionsmodedriving, mklaunchoptionsshowstraffickey:@ (YES)};

[Mkmapitem Openmapswithitems:itemarray Launchoptions:dic];
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Using Mapkit for navigation

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.