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
If your database is MySQL 5.7, you can use the spatial function: ST_Distance_Sphere() to calculate the shortest distance (unit: meters) between two points on the earth.
As follows:
MySQL version 5.6 can use ST_Distance() to calculate distance. It should be noted that it calculates the straight-line distance between two 2-dimensional coordinate points, and the result needs to be multiplied by 111195 (radius of the earth 6371000*PI/180) to convert the value into meters.