How can I draw a route between user's current location to a specific location using MapKit in Swift?
I searched a lot, but didn't find any helpful Swift-specific links or tutorials.
How can I draw a route between user's current location to a specific location using MapKit in Swift?
I searched a lot, but didn't find any helpful Swift-specific links or tutorials.
Swift 4
class MapController: UIViewController, MKMapViewDelegate {
// MARK: - showRouteOnMap
func showRouteOnMap(pickupCoordinate: CLLocationCoordinate2D, destinationCoordinate: CLLocationCoordinate2D) {
let sourcePlacemark = MKPlacemark(coordinate: pickupCoordinate, addressDictionary: nil)
let destinationPlacemark = MKPlacemark(coordinate: destinationCoordinate, addressDictionary: nil)
let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
let destinationMapItem = MKMapItem(placemark: destinationPlacemark)
let sourceAnnotation = MKPointAnnotation()
if let location = sourcePlacemark.location {
sourceAnnotation.coordinate = location.coordinate
}
let destinationAnnotation = MKPointAnnotation()
if let location = destinationPlacemark.location {
destinationAnnotation.coordinate = location.coordinate
}
self.mapView.showAnnotations([sourceAnnotation,destinationAnnotation], animated: true )
let directionRequest = MKDirectionsRequest()
directionRequest.source = sourceMapItem
directionRequest.destination = destinationMapItem
directionRequest.transportType = .automobile
// Calculate the direction
let directions = MKDirections(request: directionRequest)
directions.calculate {
(response, error) -> Void in
guard let response = response else {
if let error = error {
print("Error: \(error)")
}
return
}
let route = response.routes[0]
self.mapView.add((route.polyline), level: MKOverlayLevel.aboveRoads)
let rect = route.polyline.boundingMapRect
self.mapView.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)
}
}
// MARK: - MKMapViewDelegate
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(overlay: overlay)
renderer.strokeColor = UIColor(red: 17.0/255.0, green: 147.0/255.0, blue: 255.0/255.0, alpha: 1)
renderer.lineWidth = 5.0
return renderer
}
mapView.delegate = self
to use the map delegate –
Giannini class MapController: UIViewController, MKMapViewDelegate {
func showRouteOnMap() {
let request = MKDirectionsRequest()
request.source = MKMapItem(placemark: MKPlacemark(coordinate: annotation1.coordinate, addressDictionary: nil))
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: annotation2.coordinate, addressDictionary: nil))
request.requestsAlternateRoutes = true
request.transportType = .Automobile
let directions = MKDirections(request: request)
directions.calculateDirectionsWithCompletionHandler { [unowned self] response, error in
guard let unwrappedResponse = response else { return }
if (unwrappedResponse.routes.count > 0) {
self.mapView.addOverlay(unwrappedResponse.routes[0].polyline)
self.mapView.setVisibleMapRect(unwrappedResponse.routes[0].polyline.boundingMapRect, animated: true)
}
}
}
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if overlay is MKPolyline {
var polylineRenderer = MKPolylineRenderer(overlay: overlay)
polylineRenderer.strokeColor = UIColor.blueColor()
polylineRenderer.lineWidth = 5
return polylineRenderer
}
return nil
}
The return is an array of possible routes, usually we just want to show the first. The annotations are the map annotations.
Swift 5 + Extension based on bdelrahman Mohamed's answer
extension MKMapView {
func showRouteOnMap(pickupCoordinate: CLLocationCoordinate2D, destinationCoordinate: CLLocationCoordinate2D) {
let sourcePlacemark = MKPlacemark(coordinate: pickupCoordinate, addressDictionary: nil)
let destinationPlacemark = MKPlacemark(coordinate: destinationCoordinate, addressDictionary: nil)
let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
let destinationMapItem = MKMapItem(placemark: destinationPlacemark)
let sourceAnnotation = MKPointAnnotation()
if let location = sourcePlacemark.location {
sourceAnnotation.coordinate = location.coordinate
}
let destinationAnnotation = MKPointAnnotation()
if let location = destinationPlacemark.location {
destinationAnnotation.coordinate = location.coordinate
}
self.showAnnotations([sourceAnnotation,destinationAnnotation], animated: true )
let directionRequest = MKDirections.Request()
directionRequest.source = sourceMapItem
directionRequest.destination = destinationMapItem
directionRequest.transportType = .automobile
// Calculate the direction
let directions = MKDirections(request: directionRequest)
directions.calculate {
(response, error) -> Void in
guard let response = response else {
if let error = error {
print("Error: \(error)")
}
return
}
let route = response.routes[0]
self.addOverlay((route.polyline), level: MKOverlayLevel.aboveRoads)
let rect = route.polyline.boundingMapRect
self.setRegion(MKCoordinateRegion(rect), animated: true)
}
}}
override func viewDidLoad() { super.viewDidLoad()
mapView.delegate = self
let sourceLocation = CLLocationCoordinate2D(latitude: 22.4649, longitude: 69.0702)
let destinationLocation = CLLocationCoordinate2D(latitude: 23.0225, longitude: 72.5714)
let span = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)
let sourceRegion = MKCoordinateRegion(center: sourceLocation, span: span)
mapView.setRegion(sourceRegion, animated: true)
let destinationRegion = MKCoordinateRegion(center: destinationLocation, span: span)
mapView.setRegion(destinationRegion, animated: true)
let sourcePin = MKPointAnnotation()
sourcePin.coordinate = sourceLocation
mapView.addAnnotation(sourcePin)
let destinationPin = MKPointAnnotation()
destinationPin.coordinate = destinationLocation
mapView.addAnnotation(destinationPin)
let sourcePlacemark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil)
let destinationPlacemark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)
let directionRequest = MKDirections.Request()
directionRequest.source = MKMapItem(placemark: sourcePlacemark)
directionRequest.destination = MKMapItem(placemark: destinationPlacemark)
directionRequest.transportType = .automobile
let directions = MKDirections(request: directionRequest)
directions.calculate { (response, error) in
guard let response = response else {
if let error = error {
print("Error: \(error)")
}
return
}
let route = response.routes[0]
self.mapView.addOverlay((route.polyline), level: MKOverlayLevel.aboveRoads)
let rect = route.polyline.boundingMapRect
self.mapView.setRegion(MKCoordinateRegion(rect), animated: true)
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(overlay: overlay)
renderer.strokeColor = UIColor.red
renderer.lineWidth = 4.0
return renderer
}
© 2022 - 2024 — McMap. All rights reserved.