Is there a way to know the estimated time between 2 CLLocations and that traffic is taken into account in that calculation?
Is there a way to know the estimated time between 2 CLLocations and that traffic is taken into account in that calculation?
Yes, it is possible but with nuances. The topic of traffic can not be directly configured, but you must know that route calculation is always done according to the current conditions of the route, that is, traffic is also taken into account if it is available in that route. zone.
For example, to calculate the route between two points you can do it like this:
let source = MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: 48.855990, longitude: 2.350923), addressDictionary: nil)
let destination = MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: 48.845572, longitude: 2.312584), addressDictionary: nil)
let request = MKDirectionsRequest()
request.source = MKMapItem(placemark: source)
request.destination = MKMapItem(placemark: destination)
request.transportType = .Automobile
let directions = MKDirections(request: request)
directions.calculateDirectionsWithCompletionHandler { (response, error) in
guard let res = response where res.routes.count != 0 else {
// No hay rutas
return
}
// Cogemos la primera ruta (por ejemplo) y mostramos los datos
let ruta0 = res.routes.first
print(ruta0?.distance) // Distancia en metros
print(ruta0?.expectedTravelTime) // Tiempo en segundos
}
With this you can get the main data such as distance and estimated time of arrival for each route you return.
If you want to return more than one route as an alternative, you must use the following property:
request.requestsAlternateRoutes = true
On the other hand, to finish improving the calculation and give the maximum possible information, you can play with the properties request.departureDate
and request.arrivalDate
so that you can get the estimated time based on a specific time. If you use them and the departure date is in the future (for example), you will take into account the "usual" or estimated traffic of that route and you can take alternative routes. With the other property the same, if you set the arrival time, it will give you the best departure time based on the usual traffic.
Finally, if this is not enough, use Google Maps SDK for iOS with the that you can get all this information and more.