-->

How does the distanceFromLocation method work?

2019-05-24 13:19发布

问题:

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?

回答1:

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.



回答2:

From the documentation:

distanceFromLocation:

This method measures the distance between the two locations by tracing a line between them that follows the curvature of the Earth. The resulting arc is a smooth curve and does not take into account specific altitude changes between the two locations.

So yes, I assume it is using the Haversine Formula (or a modification of it).



回答3:

Have you used Instruments and measured it? Until you have done, it's pointless.

You can take shortcuts. Let's say you want the nearest point. Find a formula that gives you roughly the right result. Usually there's a square root involved, so get a formula for the square of the distance - that's quicker and works just as well. Find the nearest point with your formula. Now say the nearest point is 178.96 meters apart according to your formula. You can then check all points that are say less than 180 meters away with the exact formula.



回答4:

For small distances and on iOS 9, I have found that the values obtained by distanceFromLocation are reliably close to those found by the Vincenty formula using the WGS-84 ellipsoid. In my experience, they are accurate within about 7 or 8 significant figures.

A spherical model, such as the law of cosines or the Haversine formula, does not compare well for small distances.

For more information, see the geopy documentation and a table of values for comparison.