to draw path between two lat long, you can use below code.
viewcontroller.h
#import <uikit/uikit.h> #import <mapkit/mapkit.h> @interface viewcontroller : uiviewcontroller<mkmapviewdelegate> @property (strong, nonatomic) iboutlet mkmapview *mymapview; @end
viewcontroller.m
#import "viewcontroller.h" @interface viewcontroller () @property (strong, nonatomic) mkplacemark *destination; @property (strong,nonatomic) mkplacemark *source; @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; // do any additional setup after loading the view. [self getdirections]; } -(void)getdirections { cllocationcoordinate2d sourcecoords = cllocationcoordinate2dmake(37.773972, -122.431297); mkcoordinateregion region; //set zoom level using span mkcoordinatespan span; region.center = sourcecoords; span.latitudedelta = 1; span.longitudedelta = 1; region.span=span; [_mymapview setregion:region animated:true]; mkplacemark *placemark = [[mkplacemark alloc] initwithcoordinate:sourcecoords addressdictionary:nil]; mkpointannotation *annotation = [[mkpointannotation alloc] init]; annotation.coordinate = sourcecoords; annotation.title = @"san francisco"; [self.mymapview addannotation:annotation]; //[self.mymapview addannotation:placemark]; _destination = placemark; mkmapitem *mapitem = [[mkmapitem alloc] initwithplacemark:_destination]; cllocationcoordinate2d destcoords = cllocationcoordinate2dmake(37.615223, -122.389977); mkplacemark *placemark1 = [[mkplacemark alloc] initwithcoordinate:destcoords addressdictionary:nil]; mkpointannotation *annotation1 = [[mkpointannotation alloc] init]; annotation1.coordinate = destcoords; annotation1.title = @"san francisco university"; [self.mymapview addannotation:annotation1]; //[self.mymapview addannotation:placemark1]; _source = placemark1; mkmapitem *mapitem1 = [[mkmapitem alloc] initwithplacemark:_source]; mkdirectionsrequest *request = [[mkdirectionsrequest alloc] init]; request.source = mapitem1; request.destination = mapitem; request.requestsalternateroutes = no; mkdirections *directions = [[mkdirections alloc] initwithrequest:request]; [directions calculatedirectionswithcompletionhandler: ^(mkdirectionsresponse *response, nserror *error) { if (error) { nslog(@"error"); nslog(@"%@",[error localizeddescription]); } else { [self showroute:response]; } }]; } -(void)showroute:(mkdirectionsresponse *)response { for (mkroute *route in response.routes) { [_mymapview addoverlay:route.polyline level:mkoverlaylevelaboveroads]; for (mkroutestep *step in route.steps) { nslog(@"%@", step.instructions); } } } #pragma mark - mkmapviewdelegate methods - (mkoverlayrenderer *)mapview:(mkmapview *)mapview rendererforoverlay:(id<mkoverlay>)overlay { mkpolylinerenderer *renderer = [[mkpolylinerenderer alloc] initwithpolyline:overlay]; renderer.strokecolor = [uicolor colorwithred:0.0/255.0 green:171.0/255.0 blue:253.0/255.0 alpha:1.0]; renderer.linewidth = 10.0; return renderer; }
update : swift 4
import mapkit import uikit class viewcontroller: uiviewcontroller, mkmapviewdelegate { var destination: mkplacemark? var source: mkplacemark? @iboutlet var mymapview: mkmapview! func viewdidload() { super.viewdidload() // do any additional setup after loading the view. self.mymapview.delegate = self getdirections() } func getdirections() { let sourcecoords: cllocationcoordinate2d = cllocationcoordinate2dmake(37.773972, -122.431297) let region: mkcoordinateregion //set zoom level using span let span: mkcoordinatespan region.center = sourcecoords span.latitudedelta = cllocationdegrees(1) span.longitudedelta = cllocationdegrees(1) region.span = span mymapview.setregion(region, animated: true) let placemark = mkplacemark(coordinate: sourcecoords, addressdictionary: nil) let annotation = mkpointannotation() annotation.coordinate = sourcecoords annotation.title = "san francisco" mymapview.addannotation(annotation) //[self.mymapview addannotation:placemark]; destination = placemark var mapitem: mkmapitem? = nil if let adestination = destination { mapitem = mkmapitem(placemark: adestination) } let destcoords: cllocationcoordinate2d = cllocationcoordinate2dmake(37.615223, -122.389977) let placemark1 = mkplacemark(coordinate: destcoords, addressdictionary: nil) let annotation1 = mkpointannotation() annotation1.coordinate = destcoords annotation1.title = "san francisco university" mymapview.addannotation(annotation1) //[self.mymapview addannotation:placemark1]; source = placemark1 var mapitem1: mkmapitem? = nil if let asource = source { mapitem1 = mkmapitem(placemark: asource) } let request = mkdirectionsrequest() request.source = mapitem1 request.destination = mapitem request.requestsalternateroutes = false let directions = mkdirections(request: request) directions.calculate(completionhandler: {(_ response: mkdirectionsresponse, _ error: error?) -> void in if error != nil { print("error") print("(error?.localizeddescription ?? "")") } else { self.showroute(response) } }) } func showroute(_ response: mkdirectionsresponse?) { for route: mkroute in response?.routes { mymapview.add(route.polyline, level: .aboveroads) for step: mkroutestep in route.steps { print("(step.instructions)") } } } // mark: - mkmapviewdelegate methods func mapview(_ mapview: mkmapview, rendererfor overlay: mkoverlay) -> mkoverlayrenderer { var renderer: mkpolylinerenderer? = nil if let anoverlay = overlay as? mkpolyline { renderer = mkpolylinerenderer(polyline: anoverlay) } renderer?.strokecolor = uicolor(red: 0.0 / 255.0, green: 171.0 / 255.0, blue: 253.0 / 255.0, alpha: 1.0) renderer?.linewidth = 10.0 if let arenderer = renderer { return arenderer } return mkoverlayrenderer() } }
hope it will help you out! change lat long in above code as per your requirement.
to draw path between two lat long, you can use below code.
viewcontroller.h
viewcontroller.m
update : swift 4
hope it will help you out! change lat long in above code as per your requirement.