2

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)!)
hussnain ahmad
  • 440
  • 8
  • 24

1 Answers1

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,262
  • 1
  • 19
  • 31
  • does it calculate accurate distance ? – hussnain 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