MySQL 函数确定邮政编码的邻近度/范围
我已经能够创建 php 函数来确定给定邮政编码的特定范围内的邮政编码列表。然而,我的下一个任务有点复杂。
假设该表具有以下列:
id,
username
info
latitude
longitude
range
每条记录都有一个唯一的行 ID、一些信息、纬度、经度以及该人员希望查找此条目的坐标的最大范围。因此,如果我创建一个坐标为 -20, 50、范围为 15 的条目,则意味着我只希望坐标 -20, 50 15 英里以内的人能够看到我的条目。找出运行搜索的用户的纬度和经度是一件微不足道的事情。
当用户在数据库中搜索时,应检索所有记录以获取用户纬度/经度范围值内的纬度和经度坐标。
因此,使用距离公式来说明这一点的非功能代码示例是
$userLat
= 运行搜索的用户的纬度
$userLong
= 经度运行搜索的用户的信息
SELECT info FROM table
WHERE sqrt(($userLat - lat)^2 - ($userLong - long)^2) <= range
ORDER BY username ASC
这将是理想的,但从编码的角度来看它是无效的。有没有办法在一个查询中使用 PHP 和 MySQL 来运行上述比较?这将涉及能够使用给定行中的多个列值运行操作。
更新+解决方案
我创建了另一个问题条目来解决这个问题,并且有一个非常紧凑的函数来完成这个问题想要的事情。 coderpros 给出的函数是它的灵感来源(他的函数对于某些情况有更好的处理)。但是,由于我使用的函数对输入有很大程度的控制,因此我创建了一个单独的函数。它可以在下面的链接中找到:
I have been able to create php function to determine a list of zip codes within a certain range of a given zip code. However, my next task is a bit more complex.
Lets say the table has the following columns:
id,
username
info
latitude
longitude
range
Each record has a unique row id, some information, a latitude, a longitude, and a maximum range from those coordinates that the person wants this entry to be found for. So, if I create an entry with the coordinates -20, 50 with a range of 15, that means I only want people within 15 miles of the coordinates -20, 50 to be able to see my entry. Figuring out the latitude and longitude of the user running the search is a trivial matter.
When a user is searching through the database, all records should be retrieved for latitude and longitude coordinates within the value of range from the users lat/long.
So, a non-functional example of code using the distance formula to illustrate this would be
$userLat
= The latitude of the user that is running the search
$userLong
= The longitude of the user that is running the search
SELECT info FROM table
WHERE sqrt(($userLat - lat)^2 - ($userLong - long)^2) <= range
ORDER BY username ASC
That would be ideal, but it is not valid from a coding standpoint. Is there any way to run the above comparisons using PHP and MySQL in one query? This would involve being able to run operations using multiple column values from a given row.
UPDATE + SOLUTION
I created another question entry that addresses this issue and has a very compact function to do what this question wanted. The function given by coderpros served as the inspiration for it (his function has a lot better handling for certain situations). However, since I am using my function with a great degree of control over the input, I created a separate function. It can be found at the link below:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我编写的这个漂亮的 lil mySQL 函数绝对可以满足您的需要。
它以 originLatitude、originLongitude、relativeLatitude、relativeLongitude 和measure 作为参数。度量可以简单地是
英里
或公里
希望有帮助!
This nifty lil mySQL function that I wrote should definitely do the trick for you.
It takes originLatitude, originLongitude, relativeLatitude, relativeLongitude, and measure as parameters. Measure can simply be
Miles
orKilometers
Hope that helps!