0
votes

i want to search near by location with given latitude and longitude in mysql latitude is : 26.902 longitude is : 75.793 and distance is : 30

Query is :

SELECT
  id, (
    3959 * acos (
      cos ( radians(26.902) )
      * cos( radians( latitude ) )
      * cos( radians( longitude ) - radians(75.793) )
      + sin ( radians(26.902) )
      * sin( radians( latitude ) )
    )
  ) AS distance
FROM business
HAVING distance < 30
ORDER BY distance
LIMIT 0 , 20;

Result: i am getting a record with distance is 3.58, record lat/long are 26.89 / 75.74

but when i check online on other site i got distance 5.759 miles .

1
Your formula gives the straight-line distance... i.e., through the earth. You need the Haversine formula. Of course even that isn't accurate because the earth is an oblate spheroid!Nathan MacInnes
thanks for your ans but i need solution codeGulshan Prajapati
if you find any solution the let me knowGulshan Prajapati
Your SQL uses Spherical Law of Cosines to find distance between 2 points on earth surface(straight-line). Your result seems to be in the right ball park. The larger distance is probably that of route distance between 2 points. There is no easy way to calculate this from lat/lng points.david strachan

1 Answers

0
votes

Some code available here as a Stored Function: http://mysql.rjweb.org/doc.php/latlng .