在我的 php 应用程序中,我想获取给定邮政编码的最近邮政编码。这意味着我输入一个邮政编码,因为680721
我想从我的数据库中获取最近的邮政编码。
我怎样才能做到这一点?
这是我用于商店邮政编码的表格。
这varpin
是邮政编码字段。
在我的 php 应用程序中,我想获取给定邮政编码的最近邮政编码。这意味着我输入一个邮政编码,因为680721
我想从我的数据库中获取最近的邮政编码。
我怎样才能做到这一点?
这是我用于商店邮政编码的表格。
这varpin
是邮政编码字段。
说了这么多,快速浏览一下 UK Postcodes Wikipedia 条目上的“External Links”,我很快找到了 Paul Jenkins 的一篇题为 UK Post Code Distance Calculation in PHP 的文章,太棒了,你甚至可以在这里下载( uk_postcode_calc.zip)。经过简短的检查,这似乎与锡上所说的完全一样,并且只是计算了距离。但是,通过快速 google 进行 php 距离计算,您可以快速发现距离计算有更精细的等价物。我认为改用其中一个可能是个好主意。经过一番调整,这是我最后想出的:
function distance($lat1, $lon1, $lat2, $lon2, $u=’1′) {
$u=strtolower($u);
if ($u == ‘k’) { $u=1.609344; } // kilometers
elseif ($u == ‘n’) { $u=0.8684; } // nautical miles
elseif ($u == ‘m’) { $u=1; } // statute miles (default)
$d=sin(deg2rad($lat1))*sin(deg2rad($lat2))+cos(deg2rad($lat1))*cos(deg2rad($lat2))*cos(deg2rad($lon1-$lon2));
$d=rad2deg(acos($d));
$d=$d*60*1.1515;
$d=($d*$u); // apply unit
$d=round($d); // optional
return $d;
}
所以,这就是完成的困难部分(数据库和数学),接下来是一个简单的例子,使用这些信息从我们输入的邮政编码到我们提供的邮政编码数组中“找到最接近的”......为了找到“最接近的”邮政编码,有效地我们要做的是找到邮政编码之间的“最短”距离,或者只是结果中的最小数字,假设我们将结果放入一个数组中,其中键作为邮政编码,距离作为值。我们所要做的就是创建一个简单的脚本,它会在给定的数组中找到最小的数字,然后返回适当的键。简单的!
function closest ($needle,$haystack) {
if (!$needle || !$haystack) { return; }
if (!is_array($haystack)) { return; }
$smallest=min($haystack); //smallest value
foreach ($haystack as $key => $val) {
if ($val == $smallest) { return $key; }
}
}
上面的脚本正是我们想要的,使用“min”函数我们可以快速计算出我们需要返回的内容。剩下的唯一任务是将所有这些绑定在一起,我们需要创建两个函数: 使用邮政编码获取距离,从数据库中获取经度和纬度。创建一个以邮政编码为键、距离为值的数组。很简单!功能一、邮编距离
function postcode_distance ($from,$to) {
// Settings for if you have a different database structure
$table=’postcodes_uk’;
$lat=’lat’;
$lon=’lon’;
$postcode=’postcode’;
// This is a check to ensure we have a database connection
if (!@mysql_query(‘SELECT 0′)) { return; }
// Simple regex to grab the first part of the postcode
preg_match(‘/[A-Z]{1,2}[0-9R][0-9A-Z]?/’,strtoupper($from),$match);
$one=$match[0];
preg_match(‘/[A-Z]{1,2}[0-9R][0-9A-Z]?/’,strtoupper($to),$match);
$two=$match[0];
$sql = “SELECT `$lat`, `$lon` FROM `$table` WHERE `$postcode`=’$one’”;
$query = mysql_query($sql);
$one = mysql_fetch_row($query);
$sql = “SELECT `$lat`, `$lon` FROM `$table` WHERE `$postcode`=’$two’”;
$query = mysql_query($sql);
$two = mysql_fetch_row($query);
$distance = distance($one[0], $one[1], $two[0], $two[1]);
// For debug only…
//echo “The distance between postcode: $from and postcode: $to is $distance miles\n”;
return $distance;
}
Function 2, Postcode Closest
function postcode_closest ($needle,$haystack) {
if (!$needle || !$haystack) { return; }
if (!is_array($haystack)) { return; }
foreach ($haystack as $postcode) {
$results[$postcode]=postcode_distance($needle,$postcode);
}
return closest($needle,$results);
}
因此,完成后,将上述 4 个函数放入“postcode.php”之类的文件中,以便在现实世界中使用…… 测试用例:
<?php
include_once(‘postcode.php’);
if ($_POST) {
include_once(‘db.php’);
$postcodes=array(‘TF9 9BA’,'ST4 3NP’);
$input=strtoupper($_POST['postcode']);
$closest=postcode_closest($input,$postcodes);
}
if (isset($closest)) {
echo “The closest postcode is: $closest”;
}
?>
<form action=”" method=”post”>
Postcode: <input name=”postcode” maxlength=”9″ /><br />
<input type=”submit” />
</form>
您可以在此处下载此脚本:postcode_search.phps 注意:在上述测试案例中,我有一个“db.php”文件,其中包含我的数据库详细信息并启动数据库连接。我建议你这样做。确保您的数据库已填充,您应该能够使用 Paul Jenkins 的 UK Postcode csv,允许您使用自己的表结构。好了,这就是所有人,我现在可以使用这个脚本来提供与“最近”邮政编码匹配的任何位置。