Reputation: 397
I'm trying to get all the possible latitudes and longitudes withing some meter range from a given latitude and longitude. The following is the MySQL query I found. But the following one is checking distance in KM. I want in meters. Can somebody help how to modify the query so that I get directly within certain meters and not KM?
SELECT id, location_name,
(6371 * acos( cos( radians(@myLat) ) * cos( radians(latitude) ) * cos( radians(longitude) - radians(@myLng)) + sin(radians(@myLat)) * sin(radians(latitude)) )) as distance FROM sample.locations
HAVING distance <= 50
Here 50 is KM and not meters. How to give in meters? I cannot always convert meters to KM in this query as my data is in meters. Can somebody help?
Upvotes: 0
Views: 329
Reputation: 161334
6371 is the radius of the earth in km. Change that to meters. From wikipedia
For Earth, the mean radius is 6,371.0088 km (3,958.7613 mi).[16]
6,371.0088 km = 6,371,008.8 m
SELECT id, location_name,
(6371008.8 * acos( cos( radians(@myLat) ) * cos( radians(latitude) ) * cos( radians(longitude) - radians(@myLng)) + sin(radians(@myLat)) * sin(radians(latitude)) )) as distance FROM sample.locations
HAVING distance <= 50
Upvotes: 2