I frequently use the distanceFromLocation
method for CLLocation
objects to get their distance from other locations. Enumerating through an array of CLLocations, I then compare each to my reference location using this method.
I'm curious to know the processing/memory implications for using distanceFromLocation
, especially for a large number of CLLocation
objects in succession. How does this method work - does it connect to the server to get the data, or does it calculate the distance based on some mathematical formula, such as the Haversine Formula?
Is there a more efficient method to compare distances between 1 reference location and an array of CLLocation objects?
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.