MySQL 函数确定邮政编码的邻近度/范围

发布于 2024-09-15 03:44:03 字数 1045 浏览 6 评论 0原文

我已经能够创建 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 给出的函数是它的灵感来源(他的函数对于某些情况有更好的处理)。但是,由于我使用的函数对输入有很大程度的控制,因此我创建了一个单独的函数。它可以在下面的链接中找到:

MySQL 用户定义纬度经度语法函数

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:

MySQL User Defined Function for Latitude Longitude Syntax

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↙温凉少女 2024-09-22 03:44:03

我编写的这个漂亮的 lil mySQL 函数绝对可以满足您的需要。

BEGIN
   DECLARE x decimal(18,15);
   DECLARE EarthRadius decimal(7,3);
   DECLARE distInSelectedUnit decimal(18, 10);

   IF originLatitude = relativeLatitude AND originLongitude = relativeLongitude THEN
          RETURN 0; -- same lat/lon points, 0 distance
   END IF;

   -- default unit of measurement will be miles
   IF measure != 'Miles' AND measure != 'Kilometers' THEN
          SET measure = 'Miles';
   END IF;

   -- lat and lon values must be within -180 and 180.
   IF originLatitude < -180 OR relativeLatitude < -180 OR originLongitude < -180 OR relativeLongitude < -180 THEN
          RETURN 0;
   END IF;

   IF originLatitude > 180 OR relativeLatitude > 180 OR originLongitude > 180 OR relativeLongitude > 180 THEN
         RETURN 0;
   END IF;

   SET x = 0.0;

   -- convert from degrees to radians
   SET originLatitude = originLatitude * PI() / 180.0,
       originLongitude = originLongitude * PI() / 180.0,
       relativeLatitude = relativeLatitude * PI() / 180.0,
       relativeLongitude = relativeLongitude * PI() / 180.0;

   -- distance formula, accurate to within 30 feet
   SET x = Sin(originLatitude) * Sin(relativeLatitude) + Cos(originLatitude) * Cos(relativeLatitude) * Cos(relativeLongitude - originLongitude);

   IF 1 = x THEN
          SET distInSelectedUnit = 0; -- same lat/long points
          -- not enough precision in MySQL to detect this earlier in the function
   END IF;

   SET EarthRadius = 3963.189;
   SET distInSelectedUnit = EarthRadius * (-1 * Atan(x / Sqrt(1 - x * x)) + PI() / 2);

   -- convert the result to kilometers if desired
   IF measure = 'Kilometers' THEN
          SET distInSelectedUnit = MilesToKilometers(distInSelectedUnit);
   END IF;

   RETURN distInSelectedUnit;
END

它以 originLatitude、originLongitude、relativeLatitude、relativeLongitude 和measure 作为参数。度量可以简单地是英里公里

希望有帮助!

This nifty lil mySQL function that I wrote should definitely do the trick for you.

BEGIN
   DECLARE x decimal(18,15);
   DECLARE EarthRadius decimal(7,3);
   DECLARE distInSelectedUnit decimal(18, 10);

   IF originLatitude = relativeLatitude AND originLongitude = relativeLongitude THEN
          RETURN 0; -- same lat/lon points, 0 distance
   END IF;

   -- default unit of measurement will be miles
   IF measure != 'Miles' AND measure != 'Kilometers' THEN
          SET measure = 'Miles';
   END IF;

   -- lat and lon values must be within -180 and 180.
   IF originLatitude < -180 OR relativeLatitude < -180 OR originLongitude < -180 OR relativeLongitude < -180 THEN
          RETURN 0;
   END IF;

   IF originLatitude > 180 OR relativeLatitude > 180 OR originLongitude > 180 OR relativeLongitude > 180 THEN
         RETURN 0;
   END IF;

   SET x = 0.0;

   -- convert from degrees to radians
   SET originLatitude = originLatitude * PI() / 180.0,
       originLongitude = originLongitude * PI() / 180.0,
       relativeLatitude = relativeLatitude * PI() / 180.0,
       relativeLongitude = relativeLongitude * PI() / 180.0;

   -- distance formula, accurate to within 30 feet
   SET x = Sin(originLatitude) * Sin(relativeLatitude) + Cos(originLatitude) * Cos(relativeLatitude) * Cos(relativeLongitude - originLongitude);

   IF 1 = x THEN
          SET distInSelectedUnit = 0; -- same lat/long points
          -- not enough precision in MySQL to detect this earlier in the function
   END IF;

   SET EarthRadius = 3963.189;
   SET distInSelectedUnit = EarthRadius * (-1 * Atan(x / Sqrt(1 - x * x)) + PI() / 2);

   -- convert the result to kilometers if desired
   IF measure = 'Kilometers' THEN
          SET distInSelectedUnit = MilesToKilometers(distInSelectedUnit);
   END IF;

   RETURN distInSelectedUnit;
END

It takes originLatitude, originLongitude, relativeLatitude, relativeLongitude, and measure as parameters. Measure can simply be Miles or Kilometers

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文