java - sql语句查询经纬度范围
ringa_lee
ringa_lee 2017-04-17 17:56:21
0
4
501

最近在做查询指定经纬度范围的数据;问题不知如何下手,于是网上找了点资料,其中有些不懂的地方希望大家能给点想法!

问题是这样的:
sql语句查询经纬度范围

指定一个经纬度,给定一个范围值(单位:千米),查出在经纬度周围这个范围内的数据。
经度:113.914619
纬度:22.50128
范围:2km
longitude为数据表经度字段
latitude为数据表纬度字段
SQL在mysql下测试通过,其他数据库可能需要修改
SQL语句如下:

select * from location where sqrt( ( ((113.914619-longitude)*PI()*12656*cos(((22.50128+latitude)/2)*PI()/180)/180) * ((113.914619-longitude)*PI()*12656*cos (((22.50128+latitude)/2)*PI()/180)/180) ) + ( ((22.50128-latitude)*PI()*12656/180) * ((22.50128-latitude)*PI()*12656/180) ) )<2

其中的12656是啥意思啊,怎么来的呀?如果有大神整体解释下那就更好了!!

ringa_lee
ringa_lee

ringa_lee

reply all(4)
迷茫

You can refer to geohash, it should be more efficient
http://www.cnblogs.com/LBSer/p/3310455.html

刘奇

You will know the distance by calculating the distance between two points on the sphere. It is a formulad(x1,y1,x2,y2)=r*arccos(sin(x1)*sin(x2)+cos(x1)*cos(x2)*cos(y1-y2))
Add the conversion unit of the distance and you will know the value by yourself

黄舟

Semantically it should be 1经度(纬度)= 12656米. However, this is only an approximate value, especially for longitude. The higher the latitude, the smaller the value. In the extreme case, it is 0 at the pole.

This SQL is extremely inefficient and requires scanning the entire table. You can consider using spatial indexes. For MySQL, you can learn about the MySQL Spatial。MongoDB对空间数据的支持比较好,内置了GeoHash function.

阿神

The problem was solved after reading this article http://tech.meituan.com/lucene-distance....

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!