zdestiny - 7 months ago 33

iOS Question

I frequently use the

`distanceFromLocation`

`CLLocation`

I'm curious to know the processing/memory implications for using

`distanceFromLocation`

`CLLocation`

Is there a more efficient method to compare distances between 1 reference location and an array of CLLocation objects?

Answer

They are likely are using the Spherical Law of Cosines instead of the Haversine (why? see this question).

If all you want to do is compare many points against one point to see which is closest, then maybe you don't care about the accuracy of the computed distance and just about performance. In that case perhaps using Pythagoras' theorem would work for you.

All of these algorithms are detailed on this web page, which says in part:

```
If performance is an issue and accuracy less important, for small
distances Pythagorasâ€™ theorem can be used on an equirectangular
projection:*
```

You could implement a function using Pythagoras' theorem then benchmark it against the one in CLLocation and against my implementation of distanceInMetersFromRadians that uses the Spherical Law of Cosines to see how much performance difference there is.