Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate distance and ETA from maps in swift

Tags:

ios

swift

maps

I am currently using this code below that will open maps with driving guide to a certain destination.

let lat1 : NSString = "57.619302"
let lng1 : NSString = "11.954928"

let latitute:CLLocationDegrees =  lat1.doubleValue
let longitute:CLLocationDegrees =  lng1.doubleValue

let regionDistance:CLLocationDistance = 10000
let coordinates = CLLocationCoordinate2DMake(latitute, longitute)
let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
let options = [
    MKLaunchOptionsMapCenterKey: NSValue(MKCoordinate: regionSpan.center),
    MKLaunchOptionsMapSpanKey: NSValue(MKCoordinateSpan: regionSpan.span),
    MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving
]

let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = "Timo's Crib"

dispatch_async(dispatch_get_main_queue()) {
    mapItem.openInMapsWithLaunchOptions(options)
}

}))

Now when the maps are opened you can see information such as ETA and total distance to destination. How can i extract or calculate that exact information? I only saw examples in objective c but never in swift.

like image 862
Timo Cengiz Avatar asked Feb 09 '23 05:02

Timo Cengiz


1 Answers

You can do it using MKDirectionsRequest. First we need our current location, since from your code we see that you have the destination. I'm using CLLocationManagerDelegate method locationManager(_:didUpdateLocations:).

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let sourcePlacemark = MKPlacemark(coordinate: locations.last!.coordinate, addressDictionary: nil)
    let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
    ..
}

Perfect. Now we have source item and destination item. Now just the request (to simplify things, I put your code inside the method, but the logic behind destination place should be probably extracted from this method):

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    // Get current position
    let sourcePlacemark = MKPlacemark(coordinate: locations.last!.coordinate, addressDictionary: nil)
    let sourceMapItem = MKMapItem(placemark: sourcePlacemark)

    // Get destination position
    let lat1: NSString = "57.619302"
    let lng1: NSString = "11.954928"
    let destinationCoordinates = CLLocationCoordinate2DMake(lat1.doubleValue, lng1.doubleValue)
    let destinationPlacemark = MKPlacemark(coordinate: destinationCoordinates, addressDictionary: nil)
    let destinationMapItem = MKMapItem(placemark: destinationPlacemark)

    // Create request
    let request = MKDirectionsRequest()
    request.source = sourceMapItem
    request.destination = destinationMapItem
    request.transportType = MKDirectionsTransportType.Automobile
    request.requestsAlternateRoutes = false
    let directions = MKDirections(request: request)
    directions.calculateDirectionsWithCompletionHandler { response, error in
        if let route = response?.routes.first {
            print("Distance: \(route.distance), ETA: \(route.expectedTravelTime)")
        } else {
            print("Error!")
        }
    }
}
like image 50
sunshinejr Avatar answered Feb 10 '23 23:02

sunshinejr