How can i query in mysql-table(where lat and long stored) that which are the nearest location to inputed location(lat and long)?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 18:38:50

I would suggest to limit your location by some conditions which can be indexed. You can do that easily as you probably don't want to list somebody from the US to somebody in Paris as nearest location.

With just a bit modification on RustamIS query you can eliminate the most of the irrelevant locations.

select * from gps_location_table t 
where
    t.x between 20.134554 and 20.334554 and t.y between 56.11455255 and 56.31455255
order by 
    sqrt((t.x - 20.234554)*(t.x - 20.234554) + (t.y - 56.21455255)*(t.y - 56.21455255)
desc

So you defined a rectangle where you want to look for the nearest people.

SELECT *, ((ACOS(SIN(23.037698 * PI() / 180) * SIN(latitude * PI() / 180) + COS(23.037698 * PI() / 180) * COS(latitude * PI() / 180) * COS((72.564835 - longitude) * PI() / 180)) * 180 / PI()) * 60 * 1.1515 * 1.609344) as distance FROM location;

Above query will return you the distance if you enter

lat = 23.037698
long = 72.564835

Now compare distance say distance > 50kms.

This has worked for me.

let long => Y and lat => X then query like this

select * from gps_location_table t 
order by 
     sqrt(pow((t.x - 20.234554),2) + pow((t.y - 56.21455255),2))
desc

answer must be something like this!!! Mention that this formula does not return correct distance, but it works, and works faster than calculating real distance.

the main meaning is that the distance between two cordinates is:

sqrt((x1-x2)^2 + (y1-y2)^2)    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!