# How to calculate the distance between 2 points in a sphere (The Earth) in Swift?

**URL:** <https://forums.swift.org/t/how-to-calculate-the-distance-between-2-points-in-a-sphere-the-earth-in-swift/40448>\
**Category:** Using Swift\
**Tags:** localization, math\
**Created:** [September 20, 2020, 3:20pm UTC](https://forums.swift.org/t/how-to-calculate-the-distance-between-2-points-in-a-sphere-the-earth-in-swift/40448 "2020-09-20T15:20:47Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luko](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/luko/32/12861_2.png) [@Luko](https://forums.swift.org/u/Luko)\
**Post date:** [September 20, 2020, 3:20pm UTC](https://forums.swift.org/t/how-to-calculate-the-distance-between-2-points-in-a-sphere-the-earth-in-swift/40448/1 "2020-09-20T15:20:47Z")

</div>

Hi there,

Given 2 points I need to calculate the distance between them on a sphere.  
How can I do something like this?

Thanks!

![](https://global.discourse-cdn.com/swift/original/2X/c/c2dde505073351cf55d25cca950804f0452a9f99.png)

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [September 20, 2020, 3:32pm UTC](https://forums.swift.org/t/how-to-calculate-the-distance-between-2-points-in-a-sphere-the-earth-in-swift/40448/2 "2020-09-20T15:32:17Z")

</div>

Import `Foundation` to access trigonometric functions in Swift, and use the standard mathematical formulas to calculate great-circle distance.

---

<div class="post-metadata">

**Author:** ![Luko](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/luko/32/12861_2.png) [@Luko](https://forums.swift.org/u/Luko)\
**Post date:** [September 20, 2020, 4:06pm UTC](https://forums.swift.org/t/how-to-calculate-the-distance-between-2-points-in-a-sphere-the-earth-in-swift/40448/3 "2020-09-20T16:06:46Z")

</div>

Hi Nevin.  
I must see for here, right?

> **[Swift Numerics](https://www.swift.org/blog/numerics/)**
>
> I’m excited to announce a new open-source project for the Swift ecosystem, Swift Numerics!
> Swift Numerics will provide the building blocks of numerical computing in Swift, as a set of fine-grained modules bundled together into a single Swift...

> **[Explore numerical computing in Swift - WWDC20 - Videos - Apple Developer](https://developer.apple.com/videos/play/wwdc2020/10217/)**
>
> Meet Swift Numerics: a new Swift package for computational mathematics. Take a tour of the protocols and types available in the package...

---

<div class="post-metadata">

**Author:** ![Luko](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/luko/32/12861_2.png) [@Luko](https://forums.swift.org/u/Luko)\
**Post date:** [September 20, 2020, 5:00pm UTC](https://forums.swift.org/t/how-to-calculate-the-distance-between-2-points-in-a-sphere-the-earth-in-swift/40448/4 "2020-09-20T17:00:39Z")

</div>

I found this

```swift
func degreesToRadians(degrees: Double) -> Double {
    return degrees * Double.pi / 180
}

func distanceInKmBetweenEarthCoordinates(lat1: Double, lon1: Double, lat2: Double, lon2: Double) -> Double {

    let earthRadiusKm: Double = 6371

    let dLat = degreesToRadians(degrees: lat2 - lat1)
    let dLon = degreesToRadians(degrees: lon2 - lon1)

    let lat1 = degreesToRadians(degrees: lat1)
    let lat2 = degreesToRadians(degrees: lat2)

    let a = sin(dLat/2) * sin(dLat/2) +
    sin(dLon/2) * sin(dLon/2) * cos(lat1) * cos(lat2)
    let c = 2 * atan2(sqrt(a), sqrt(1 - a))
    return earthRadiusKm * c
}

```

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [September 20, 2020, 5:04pm UTC](https://forums.swift.org/t/how-to-calculate-the-distance-between-2-points-in-a-sphere-the-earth-in-swift/40448/5 "2020-09-20T17:04:14Z")

</div>

You can also use `CoreLocation` to do it:

```swift
import CoreLocation

let first = CLLocation(latitude: 1.1, longitude: 2.2)
let second = CLLocation(latitude: 1.1, longitude: 2.5)

let distanceInMeters = first.distance(from: second)

```

---

<div class="post-metadata">

**Author:** ![Luko](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/luko/32/12861_2.png) [@Luko](https://forums.swift.org/u/Luko)\
**Post date:** [September 20, 2020, 5:06pm UTC](https://forums.swift.org/t/how-to-calculate-the-distance-between-2-points-in-a-sphere-the-earth-in-swift/40448/6 "2020-09-20T17:06:03Z")

</div>

Oh, great!  
Thank you very much

---

<div class="post-metadata">

**Author:** ![AlexanderM](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexanderm/32/9781_2.png) [@AlexanderM](https://forums.swift.org/u/AlexanderM)\
**Post date:** [September 20, 2020, 5:33pm UTC](https://forums.swift.org/t/how-to-calculate-the-distance-between-2-points-in-a-sphere-the-earth-in-swift/40448/7 "2020-09-20T17:33:59Z")

</div>

Warning: because of its rotation pushing mass outwards at the equator, the Earth isn't a sphere, but rather an [oblate spheroid](https://en.wikipedia.org/wiki/Figure_of_the_Earth).

Approximating point-to-point distance as great-circle distance over a sphere is just that, an approximation, which gets worse over large vertical distances.

---

<div class="post-metadata">

**Author:** ![vermont42](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vermont42/32/12663_2.png) [@vermont42](https://forums.swift.org/u/vermont42)\
**Post date:** [September 20, 2020, 6:13pm UTC](https://forums.swift.org/t/how-to-calculate-the-distance-between-2-points-in-a-sphere-the-earth-in-swift/40448/8 "2020-09-20T18:13:25Z")

</div>

Interestingly, Earth’s oblate-spheroid shape [caused](https://medium.com/starts-with-a-bang/the-first-clock-in-america-failed-and-it-helped-revolutionize-physics-223f8ab845d9) colonial-era American clocks to be inaccurate.

---

<div class="post-metadata">

**Author:** ![rvsrvs](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rvsrvs/32/5270_2.png) [@rvsrvs](https://forums.swift.org/u/rvsrvs)\
**Post date:** [September 21, 2020, 6:24pm UTC](https://forums.swift.org/t/how-to-calculate-the-distance-between-2-points-in-a-sphere-the-earth-in-swift/40448/9 "2020-09-21T18:24:25Z")

</div>

You want to use Vincenty's formula if you want real accuracy, the haversine formula otherwise. A quick google search on Vincenty's formula in Swift turns up: [GitHub - dastrobu/vincenty: Compute vincenty distance in Swift](https://github.com/dastrobu/vincenty)

---

<div class="post-metadata">

**Author:** ![Luko](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/luko/32/12861_2.png) [@Luko](https://forums.swift.org/u/Luko)\
**Post date:** [September 29, 2020, 7:34pm UTC](https://forums.swift.org/t/how-to-calculate-the-distance-between-2-points-in-a-sphere-the-earth-in-swift/40448/10 "2020-09-29T19:34:06Z")

</div>

Great! Thanks
