MySQL Function to Determine Zip Code Proximity/Range

use*_*404 4 php mysql zipcode function proximity

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
Run Code Online (Sandbox Code Playgroud)

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.

当用户搜索数据库时,应检索所有记录,以获取用户lat/long范围内的纬度和经度坐标.

因此,使用距离公式的代码的非功能性示例将说明这一点

$userLat =正在运行搜索的用户的纬度

$userLong =正在运行搜索的用户的经度

SELECT info FROM table 
WHERE sqrt(($userLat - lat)^2 - ($userLong - long)^2) <= range 
ORDER BY username ASC
Run Code Online (Sandbox Code Playgroud)

这将是理想的,但从编码的角度来看它是无效的.有没有办法在一个查询中使用PHP和MySQL运行上述比较?这将涉及能够使用来自给定行的多个列值来运行操作.


更新+解决方案

我创建了另一个问题条目来解决这个问题,并且有一个非常紧凑的功能来完成这个问题.coderpros给出的功能是它的灵感(他的功能在某些情况下有更好的处理).但是,由于我使用我的函数对输入有很大程度的控制,我创建了一个单独的函数.它可以在以下链接中找到:

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

cod*_*pro 6

我写的这个漂亮的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
Run Code Online (Sandbox Code Playgroud)

它将originLatitude,originLongitude,relativeLatitude,relativeLongitude和measure作为参数.措施可以简单地MilesKilometers

希望有所帮助!