-1

I have a mySql table named tbl_places This contains fields Place_id, PlaceName, Place_LATITUDE, place_LONGITUDE.

I want to use it in the place detail page now If the current place_id of the open page is 4 then I want a maximum 6 records nearest to that place.

Suppose the current place is:

 place_id = 4
 place_name = Lal Killa
 place_LATITUDE = 77.2413969039917
 place_LONGITUDE = 28.653838307772872

I can't understand how to find nearest place to the current place??

Joe Miller
  • 3,843
  • 1
  • 23
  • 38
chirag lathiya
  • 79
  • 2
  • 2
  • 11
  • Does this answer your question? [Find nearest latitude/longitude with an SQL query](https://stackoverflow.com/questions/2234204/find-nearest-latitude-longitude-with-an-sql-query) – Vega Jan 06 '23 at 23:58

2 Answers2

1

Use below query:

SELECT
    Place_id,
    PlaceName,
    (
        3959 
        * acos(
            cos( radians(37) ) 
            * cos( radians( Place_LATITUDE ) ) 
            * cos( radians( Place_LONGITUDE ) - radians(-122) ) 
            + sin( radians(37) ) 
            * sin( radians( Place_LATITUDE ) ) 
        ) 
    ) AS distance 
FROM tbl_places 
HAVING distance < 25 
ORDER BY distance 
LIMIT 0 , 20;

NOTE - Here latitude = 37 & longitude = -122

Also, check here for more reference.

AymDev
  • 6,626
  • 4
  • 29
  • 52
Jainil
  • 1,488
  • 1
  • 21
  • 26
0

You can use a Cohen–Sutherland (point clipping) algorithm to know the people near by, if you want to minimize the result,set it as a incremental order from 0 to YOUR_LIMIT.

it is better than a formula that convert a polar co-ordinates[(ie)lat and long] to XY