1

I am working on MKMapView to get the direction between 2 locations.Is there any way i can get the time it may take in completing this journey?Is there any inbuilt property of MKRoute or MKDirection Class that can provide me time to cover this journey ?

Any help would be appreciated.

Thanks Vikas

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Vikas Ojha
  • 444
  • 1
  • 8
  • 23

2 Answers2

4

Yes.

The MKDirections method named calculateETAWithCompletionHandler: can just be a bit easy to overlook, though.

An implementation could look as follows:

MKDirections *yourDirections = ...;

[yourDirections calculateETAWithCompletionHandler:^(MKETAResponse *response, NSError *error)
{
     NSTimeInterval estimatedTravelTimeInSeconds = response.expectedTravelTime;
     ...
}];

Source:

https://developer.apple.com/library/mac/documentation/MapKit/Reference/MKDirections_class/Reference/Reference.html

jaynetics
  • 1,234
  • 12
  • 15
  • Thanks it worked. any idea about the api that can give me route for india between 2 places as Apple's Direction class does not provide it – Vikas Ojha Apr 16 '14 at 07:46
  • In this case you might want to check if these directions are available via Google maps. If they are, it might be preferable to use the Google maps SDK instead of Apple maps in your app: https://developers.google.com/maps/documentation/ios/ -- See this question about displaying directions in the Google maps SDK: http://stackoverflow.com/questions/16811965/how-to-draw-a-path-one-place-to-another-place-in-google-map-sdk-ios/20171859 – jaynetics Apr 16 '14 at 08:27
0

This is for Swift 2 if anyone is looking. Declare these variables before viewDidLoad.

var request = MKDirectionsRequest()
var directions: MKDirections!
var locationManager = CLLocationManager()

Create a private function like this:

private func estimateTravelTime(request: MKDirectionsRequest, transportType: MKDirectionsTransportType, source: MKMapItem, destination: MKMapItem, string: String -> ()) {
    request.source = source
    request.destination = destination
    request.transportType = transportType
    request.requestsAlternateRoutes = false
    directions = MKDirections(request: request)
    directions.calculateETAWithCompletionHandler { (response, error) in
        if let seconds = response?.expectedTravelTime {
            let minutes = seconds / 60
            string(Int(ceil(minutes)).description)
        }
    }
}

Now in your viewDidLoad or viewDidApear (depending on what you're after) use it like this:

    guard let currentLocation = locationManager.location?.coordinate else {return}
    let source = MKMapItem(placemark: MKPlacemark(coordinate: currentLocation, addressDictionary: nil))
    let destination = MKMapItem(placemark: MKPlacemark(coordinate: PUT_YOUR_DESTINATION_HERE, addressDictionary: nil))

    estimateTravelTime(request, transportType: .Automobile, source: source, destination: destination) { (string) in
        print("Minutes for Automobile: \(string) minutes")
    }
    estimateTravelTime(request, transportType: .Walking, source: source, destination: destination) { (string) in
        print("Minutes for Walking: \(string) minutes")
    }

For your destination just user a CLLocationCoordinate2DMake with the latitude and longitude, something like this:

let destination = CLLocationCoordinate2DMake(44.444551, 26.096641)