Select coordinates which fall within a radius of a central point?

风格不统一 提交于 2019-12-02 17:20:47

The SQL below should work:

SELECT * FROM Table1 a 
WHERE (
          acos(sin(a.Latitude * 0.0175) * sin(YOUR_LATITUDE_X * 0.0175) 
               + cos(a.Latitude * 0.0175) * cos(YOUR_LATITUDE_X * 0.0175) *    
                 cos((YOUR_LONGITUDE_Y * 0.0175) - (a.Longitude * 0.0175))
              ) * 3959 <= YOUR_RADIUS_INMILES
      )

This is based on the spherical law of cosines, for more detailed information on the topic, check out this article - http://www.movable-type.co.uk/scripts/latlong.html

You probably need to do this in two steps. Select the points that lie within a 20 mile square with it's centre at X,Y. Assuming you calculate the top,left and bottom,right coordinates of the square first you can get all the points inside the square from the database with:

select * from coordinates where longitude < right and longitude > left and 
latitude < top and latitude > bottom;

The second step is to see whether the set of points is inside the 10 mile radius circle. At this point I would be tempted to use Google maps to calculate the distance between the points and the centre of your square using the google.maps.geometry.spherical.computeDistanceBetween(from:LatLng, to:LatLng, radius?:number)function. Check the answer is less than 10 miles. This function uses the radius of the earth as a default.

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