如何到达一定半径的地点
我有一个活动数据库,每个活动可以举行3天,每一天都包含一个邮政编码。
所以数据库看起来像这样(+很多其他字段)
在另一个数据库中,我有一个地理位置信息(邮政编码、纬度、经度)
现在用户可以输入邮政编码以及其中的半径和活动将出现半径。
问题:
1 - 实现这一目标的最佳方法是什么?
记住解决方案
查看活动中所有可能的邮政编码,并将其加入到地理表中以获取其纬度/经度,
然后当用户搜索邮政编码时,获取纬度/经度和执行数学方程以获得该点附近的所有邮政编码。
但我认为就性能而言这不是一个好方法,因为我必须对 3000 多个活动应用查询
找到距离的代码
使用 MySQL 查找附近的位置(半正矢公式)
选择 id, ( 3959 * acos( cos( 弧度(37) ) * cos( 弧度( 纬度 ) ) * cos( 弧度( lng ) - 弧度(-122) ) + sin( 弧度(37) ) * sin ( 弧度( 纬度 ) ) ) ) AS 距离 FROM 标记 距离< 25 ORDER BY distance LIMIT 0 , 20;
你们觉得怎么样?
I have a database of activities , each activities could be held on 3 days , each day contains a postal code .
So the database looks like that (+ alot of other fields)
In another database i have a Geo Location info (postal code , lat , long)
Now users can enter there postal code and a radius and activities in that radius will appear.
Question :
1 - What is the best way to accomplish that ?
Solution in mind
Make a view of all possible postal codes from the activities and join it on the Geo table to get their Lat/Lng
then when a user search for a postal code , get the Lat/Lng and do the mathematical equation to get all postal codes near that point .
But i don't think in term of performance this is a good way since i will have to apply the query on 3000+ activities
Codes found for distance
Finding locations nearby with MySQL (Haversine Formula)
SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance
FROM markers
HAVING distance < 25
ORDER BY distance LIMIT 0 , 20;
What do you guys think ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要 GIS 来执行此操作(并使用空间索引),但 mysql 无法做到这一点 - mysql GIS 功能只能处理矩形。 PostreSQL 具有 GIS 功能。
最简单的确实是数学表达式。如果您可以使用一些投影坐标(纬度和经度是球体坐标),那将是最好的。在这个投影坐标系中转换整个数据库,而不仅仅是使用简单的表达式(不需要使用三角函数):
请注意,地球是一个球体,这意味着这仅适用于较小的距离(例如 <1000 公里)。但无论如何,我认为你不需要精确的圆......
You would need a GIS to do that (and use spatial index), but mysql is not capable of it - mysql GIS functionality can handle just rectangles. PostreSQL is capable of GIS.
Easiest would really be the math expression. It would be best if you could use some Projected coordinates (lat & lon are sphere coordinates). Convert whole database in this projected coordination system and than just use simple expression (without need to use trigonometric functions):
note that the Earth is a sphere, which means this will only work exactly for smaller distances (say < 1000 km). But anyway I think you don't need exact circle...
我在性能方面是错误的,查询花费了不到 0.5 秒的时间来计算 3000 多个活动的距离。
I was wrong in term of performance , the query took less than 0,5 second to calculate the distance on the 3000+ activities .