How to search by distance

Asked

Viewed 59 times

1

I have a database with coordinates of several locations. I would like to know how, from an informed location, to get only the places you are until a certain distance.

I saw a similar question: How to get distance given coordinates using SQL? However, the answer relates to finding the specific distance of two informed locations. I liked the answers in that question, but I believe there must be a more appropriate search algorithm. Something like:

SELECT * FROM foo WHERE ((@lat - foo.lat)^2 + (@long - foo.long)^2)^(1/2) < @bar

I know this is not the correct syntax for exponentiations in SQL. My biggest problem with the above formula, however, is that it should only be correct for relatively small distances, or for a flat planet.

Does the formula I seek exist? Could someone give me a light?

  • But isn’t that exactly what you have in the question you linked? In the utluiz answer you have an sql function to calculate the distance.

  • @bfavaretto the search functions I’ve seen in the answers search within a square. And it becomes more inaccurate as the distance increases. I would like a formula that actually gets any record up to a certain distance from an informed point, for that would have to be a more complex structure than a square or a circle - a calotte.

  • You must be getting confused, see the sql function in the utluiz answer. The formula even uses the radius of the Earth! It is for calculation on a sphere (and is somewhat inaccurate because the planet is not a perfect sphere, but is sufficient for most uses). Please give me a feedback on the subject, I intend to close this question as duplicate.

  • Okay, I’ve reviewed it and I agree. You can close :)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.