0

I am creating an app like an UBER and i am using MapKit ,i want to display route between two locations and for this i have use following code

    viewMap.delegate = self

    let sourceLocation = CLLocationCoordinate2D(latitude: sourceLatitude, longitude: sourceLongitude)
    let destinationLocation = CLLocationCoordinate2D(latitude: destinationLatitude, longitude: destinationLongitude)

    let sourcePlacemark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil)
    let destinationPlacemark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)

    let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
    let destinationMapItem = MKMapItem(placemark: destinationPlacemark)

    let sourceAnnotation = MKPointAnnotation()
    sourceAnnotation.title = strSource

    if let location = sourcePlacemark.location {
        sourceAnnotation.coordinate = location.coordinate
    }


    let destinationAnnotation = MKPointAnnotation()
    destinationAnnotation.title = strDestination

    if let location = destinationPlacemark.location {
        destinationAnnotation.coordinate = location.coordinate
    }

    self.viewMap.showAnnotations([sourceAnnotation,destinationAnnotation], animated: true )

    let directionRequest = MKDirectionsRequest()

    directionRequest.source = sourceMapItem
    directionRequest.destination = destinationMapItem
    directionRequest.transportType = .automobile

    let directions = MKDirections(request: directionRequest)

    directions.calculate {
        (response, error) -> Void in

        guard let response = response else {
            if let error = error {
                print("Route Error: \(error)")
            }

            return
        }

        let route = response.routes[0]
        self.viewMap.add((route.polyline), level: MKOverlayLevel.aboveRoads)

        let rect = route.polyline.boundingMapRect
        self.viewMap.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)
    }

extension HomeVC: MKMapViewDelegate

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    let renderer = MKPolylineRenderer(overlay: overlay)
    renderer.strokeColor = #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1)
    renderer.lineWidth = 2.0

    return renderer
}

I have check all the possible solution and i am also getting latitude and longitude of source and destination Locations but i still its not working and i am getting following error.

Please help me with this!

enter image description here

Khushbu Desai
  • 1,003
  • 1
  • 10
  • 32

2 Answers2

2

Apple maps directions is available to specific countries only. Please check full list here. Apple Map Availability list (Check in Maps: Directions section). You should look into Google Map SDK in your app is targeting countries not listed in above link.

iVarun
  • 6,496
  • 2
  • 26
  • 34
  • Is there any other solution or any Library for this? – Khushbu Desai Mar 05 '18 at 09:34
  • You can use Google Map SDK. Google Maps will works in all countries. – iVarun Mar 05 '18 at 09:36
  • @KhushbuDesai If this answer or any other one solved your issue, please mark it as accepted. It will helps other to find solution easily. Thanks – iVarun Mar 05 '18 at 09:59
  • @KhushbuDesai If this answer or any other one solved your issue, please mark it as accepted. It will helps other to find solution easily. Thanks – iVarun Mar 05 '18 at 11:00
2

This works for me and it draws the destination in the MK Map, also it focuses to the destination

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        let request = MKDirectionsRequest()
        request.source = MKMapItem.forCurrentLocation()
        request.destination = MKMapItem(placemark: MKPlacemark(coordinate: (view.annotation?.coordinate)!))
        request.requestsAlternateRoutes = true
        request.requestsAlternateRoutes = true
        request.transportType = .walking
        let directions = MKDirections(request: request)
        directions.calculate { [unowned self] response, error in
            guard let unwrappedResponse = response else { return }

            for route in unwrappedResponse.routes {
                self.myMapView.add(route.polyline)                self.myMapView.setVisibleMapRect(route.polyline.boundingMapRect, animated: true)
                return
            }
        }

    }


func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        let polyLine = overlay
        let polyLineRenderer = MKPolylineRenderer(overlay: polyLine)
        polyLineRenderer.strokeColor = UIColor.blue
        polyLineRenderer.lineWidth = 2.0
        return polyLineRenderer
    }

this works when you click on a MKAnnotationView, if you want to draw from another code you just have to cut the code to your function and change the line

request.destination = MKMapItem(placemark: MKPlacemark(coordinate: (view.annotation?.coordinate)!))

to

request.destination = MKMapItem(placemark: MKPlacemark(coordinate: (CLLocationCoordinate2D(/*Your Location*/))!))
Vasilis D.
  • 1,416
  • 13
  • 21