D. Coder
D. Coder

Reputation: 57

Sort MySQL records according to co-ordinates

I have a MySQL Table where there are three columns: lat, lon and ID

Now as Input I want user to give a pair of Lat & long. In return, I want to show the sorted rows from closest to farthest.

Now, one solution of this problem is, fetch all entries in an array, make a temp array, calculate distance for each item using haversine formula, push to the temp array, and finally perform any sorting algorithm on that array.

But, this operation is heavy and I want a better solution. Is there any?

P.S: I am using PHP script.

Upvotes: 0

Views: 31

Answers (1)

tony gil
tony gil

Reputation: 9564

short answer: NO.

Long answer: maybe. IF you are working within a given and VERY limited radius, you can simplify things by y establishing a fixed value for each tenth of a degree in lat and in lng and apply the hypothenuse (square root of [delta lat squared + delta lng squared]).

But this will work only for points that are quite close to your center point.

Upvotes: 1

Related Questions