search

Home  >  Q&A  >  body text

javascript - Baidu coordinates, returns the coordinates near the target

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

伊谢尔伦伊谢尔伦2773 days ago893

reply all(1)I'll reply

  • 習慣沉默

    習慣沉默2017-05-25 15:10:24

    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:

    SELECT ST_Distance_Sphere(POINT(lat,lng), POINT(116.307852,40.057031)) as distance
    FROM table 
    where distance < 1000

    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.

    reply
    0
  • Cancelreply