Member-only story

Draw polyline on Mapkit (Swift)

Mr.Javed Multani
2 min readNov 2, 2019

--

One of the project come with requirement that draw polyline between two location and it should be follow street road. After I tried lots of codes finally I got to know that. Today I am gonna show you the tutorial for draw line on map n iOS.

Lets start with Code, get some variables like :

var locRoute : MKRoute?
var directionsRequest = MKDirections.Request()var arrayarrayPlacemarks = [MKMapItem]()
var selectedPin:MKPlacemark? = nil
let locationManager = CLLocationManager()

Following is the code for draw polyline by using MKRoutes.

//Add destination placemark...arrayarrayPlacemarks.append(MKMapItem(placemark: placemark))directionsRequest.transportType = MKDirectionsTransportType.automobile//Draw polyline by using MKRoute so it follows the street roads...for (k, item) in arrayarrayPlacemarks.enumerated() {if k < (arrayarrayPlacemarks.count - 1) {directionsRequest.source = itemdirectionsRequest.destination = arrayarrayPlacemarks[k+1]let directions = MKDirections(request: directionsRequest)directions.calculate { (response:MKDirections.Response!, error: Error!) -> Void inif error == nil {self.locRoute = response.routes[0] as? MKRoutelet…

--

--

Mr.Javed Multani
Mr.Javed Multani

Written by Mr.Javed Multani

Software Engineer | Certified ScrumMaster® (CSM) | UX Researcher | Youtuber | Tech Writer

No responses yet