Now there is a table recording user coordinates (using Baidu API), with two field records, such as coordinates (116.307852, 40.057031).
Now what should we do to find all the users within 1km of the user?
I thought I could do it directly:
select *
from table
where x between 116.307852-1km and 116.307852+1km
and y between 40.057031-1km and 40.057031+1km
I’m not familiar with the coordinate API, thank you for helping me take a look.
Baidu map to find the distance between two points
如果你的数据库是MySQL 5.7的话可以使用空间函数:ST_Distance_Sphere()来计算地球上两点之间的最短距离(单位:米)。
如下:
MySQL 5.6版本可以使用 ST_Distance()来计算距离。需要注意的是,它计算的是两个2维坐标点之间的直线距离,结果需要乘111195(地球半径6371000*PI/180)将值转换为米。