0

I am currently working on an IOS project in which i have to calculate distance between two location. I have done without using google but i want to use google api to get accurate distance i am sharing my code here

    let myLocation = CLLocation(latitude: CLLocationDegrees(latittude[indexPath.row])!, longitude: CLLocationDegrees(longittude[indexPath.row])!)
        let lat = UserDefaults.standard.string(forKey: "lat") ?? ""
        let long = UserDefaults.standard.string(forKey: "long") ?? ""
        let myBuddysLocation = CLLocation(latitude: CLLocationDegrees(lat)!, longitude: CLLocationDegrees(long)!)
Hasnain ahmad
  • 301
  • 1
  • 10
  • 33

2 Answers2

2

Use distance function of CoreLocation Framework,

 var startLocation = CLLocation(latitude: startLatitude, longitude: startLongitude)
 var endLocation = CLLocation(latitude: endLatitude, longitude: endLongitude)
 var distance: CLLocationDistance = startLocation.distance(from: endLocation)
Yatendra
  • 1,310
  • 1
  • 18
  • 31
  • does it calculate accurate distance ? – Hasnain ahmad Mar 15 '19 at 08:25
  • The distance is calculated by tracing a line between the two points that follows the curvature of the Earth, and measuring the length of the resulting arc.https://developer.apple.com/documentation/corelocation/cllocation/1423689-distance – Yatendra Mar 15 '19 at 09:22
  • @Yatendra does it mean that it gives accurate distance? – Ahmed Bahgat Mar 24 '21 at 17:24
  • It gives accurate straight-line distance but it would be inaccurate for driving as an example. So it depends on your needs. – C6Silver Aug 14 '21 at 22:17
1

Swift 5+:
As far as I know, there are two ways to find the distance. If you are looking for driving distance, you can always use MKDirections. Here is the code for finding driving distance (You can also find walking, and transit distance by changing transport type).

let sourceP = CLLocationCoordinate2DMake( sourceLat, sourceLong)
let destP = CLLocationCoordinate2DMake( desLat, desLong)
let source = MKPlacemark(coordinate: sourceP)
let destination = MKPlacemark(coordinate: destP)
        
let request = MKDirections.Request()
request.source = MKMapItem(placemark: source)
request.destination = MKMapItem(placemark: destination)

// Specify the transportation type
request.transportType = MKDirectionsTransportType.automobile;

// If you want only the shortest route, set this to a false
request.requestsAlternateRoutes = true

let directions = MKDirections(request: request)

 // Now we have the routes, we can calculate the distance using
 directions.calculate { (response, error) in
    if let response = response, let route = response.routes.first {
                print(route.distance) //This will return distance in meters
    }
 }

If you are only looking for air distance/bird's eye distance/coordinate distance, you can use this code:

let sourceP = CLLocation(latitude: sourceLat, longitude: sourceLong)
let desP = CLLocation(latitude: desLat, longitude: desLong))

let distanceInMeter = sourceP.distance(from: desP)