Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot Directions between two locations in iOS Swift using mapView and CLLocationManager

I am using mapView.showUserLocation = true to plot user location and display it as an Annotation on my MKMapView object.

Now, when I have obtained the users' location, I want to plot a path from that set of coords to my destination. I tried using MKDirections for this

Here is how I was trying to do it:

func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation) {
    mapView.showsUserLocation = true
    print("test")
    //Setting Up Source Location
    let sourceLocation = self.mapView.userLocation.coordinate
    let sourcePlacemark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil)
    let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
    let sourceAnnotation = MKPointAnnotation()
    sourceAnnotation.title = "You are Here"

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

    //Setting Up Destination Location
    let destinationLocation = CLLocationCoordinate2D(latitude: 28.6621292, longitude: 77.30198310000003)
    let destinationPlacemark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)
    let destinationMapItem = MKMapItem(placemark: destinationPlacemark)
    let destinationAnnotation = MKPointAnnotation()
    destinationAnnotation.title = "Conclave is Here"

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


    self.mapView.removeAnnotations(self.mapView.annotations)
    self.mapView.showAnnotations([sourceAnnotation,destinationAnnotation], animated: true)


    //Plotting a course from Source to Destination
    let directionRequest = MKDirectionsRequest()
    directionRequest.source = sourceMapItem
    directionRequest.destination = destinationMapItem
    directionRequest.transportType = .Automobile

    // Calculate the direction
    let directions = MKDirections(request: directionRequest)

    // 8.
    directions.calculateDirectionsWithCompletionHandler {
        (response, error) -> Void 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(MKCoordinateRegionForMapRect(rect), animated: true)
    }

}

However, when I tried debugging it, I get the following error: Error: Error Domain=MKErrorDomain Code=5 "Directions Not Available" UserInfo=0x1742f7600 {NSLocalizedFailureReason=A route could not be determined between these locations., MKErrorGEOError=-403, MKDirectionsErrorCode=1, NSLocalizedDescription=Directions Not Available}

I did some research and found out that MKDirections in not available in India, so I was wondering how to can get around showing Navigation Directions to the user.

Thanks for the help!

like image 557
Ayush Gupta Avatar asked Nov 05 '16 06:11

Ayush Gupta


Video Answer


2 Answers

Error Domain=MKErrorDomain Code=5 "Directions Not Available"

You are most likely to get this error if the location doesn't belong to any of the countries in this list: http://www.apple.com/ios/feature-availability/#maps-directions

While on iOS simulator, you can easily customize your current location. Two ways to do it:

iOS Simulator -> 'Debug' tab -> Location -> {Choose}

Xcode -> 'Debug' tab -> Simulate Location -> {Choose}

So i suggest you to Use this. GoogleMaps SDK and draw route.

https://gist.github.com/himanshu-benzatine/10670936c8f16ea1ae482bc6bb684adc

like image 200
Himanshu Moradiya Avatar answered Oct 15 '22 05:10

Himanshu Moradiya


Better to use google api to draw polyline from source & destination. You can use the api

https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=%@&destinations=%@&key=

Then you get the json data of lat long(s) from where path can be drawn. Decode the json data and draw polyline in MKMapkit

like image 40
Anirban iOS Avatar answered Oct 15 '22 06:10

Anirban iOS