Php 加拿大邮政编码半径
我正在使用我在网上找到的以下脚本来获取给定坐标集之间的所有邮政编码 当使用它时,我担心的是,当一些邮政编码被抓取时,会超过输入的距离;不多——大约20公里远Php 加拿大邮政编码半径,php,mysql,postal-code,Php,Mysql,Postal Code,我正在使用我在网上找到的以下脚本来获取给定坐标集之间的所有邮政编码 当使用它时,我担心的是,当一些邮政编码被抓取时,会超过输入的距离;不多——大约20公里远 function GetPostalCodes($latitude, $longitude, $range) { $radius = 3959; $north = rad2deg(asin(sin(deg2rad($latitude)) * cos($range / $radius) + cos(deg2rad($latit
function GetPostalCodes($latitude, $longitude, $range) {
$radius = 3959;
$north = rad2deg(asin(sin(deg2rad($latitude)) * cos($range / $radius) + cos(deg2rad($latitude)) * sin($range / $radius) * cos(deg2rad(0))));
$south = rad2deg(asin(sin(deg2rad($latitude)) * cos($range / $radius) + cos(deg2rad($latitude)) * sin($range / $radius) * cos(deg2rad(180))));
$east = rad2deg(deg2rad($longitude) + atan2(sin(deg2rad(90)) * sin($range / $radius) * cos(deg2rad($latitude)), cos($range / $radius) - sin(deg2rad($latitude)) * sin(deg2rad($north))));
$west = rad2deg(deg2rad($longitude) + atan2(sin(deg2rad(270)) * sin($range / $radius) * cos(deg2rad($latitude)), cos($range / $radius) - sin(deg2rad($latitude)) * sin(deg2rad($north))));
$return = DBSelectAllArrays("SELECT postal FROM postalcodes WHERE (latitude <= $north AND latitude >= $south AND longitude <= $east AND longitude >= $west)");
krsort($return);
if (empty($return)) return false;
return $return;
}
函数GetPostalCodes($latitude、$latitude、$range){
$radius=3959;
$north=rad2deg(asin(sin(deg2rad($latitude))*cos($range/$radius)+cos(deg2rad($latitude))*sin($range/$radius)*cos(deg2rad(0));
$south=rad2deg(asin(sin(deg2rad($latitude))*cos($range/$radius)+cos(deg2rad($latitude))*sin($range/$radius)*cos(deg2rad(180));
$east=rad2deg(deg2rad($经度)+atan2(sin(deg2rad(90))*sin($range/$radius)*cos(deg2rad($纬度)),cos($range/$radius)-sin(deg2rad($纬度))*sin(deg2rad($north));
$west=rad2deg(deg2rad($经度)+atan2(sin(deg2rad(270))*sin($range/$radius)*cos(deg2rad($latitude)),cos($range/$radius)-sin(deg2rad($latitude))*sin(deg2rad($north));
$return=dbselectallarray(“从邮政编码中选择邮政编码,其中(纬度=$south,经度=$west)”;
krsort($返回);
if(空($return))返回false;
return$return;
}
为了得到更准确的结果,我是否遗漏了一些东西?鉴于您的评论:
$radius = 6371.0; // mean radius of Earth in km
这是取自维基百科,但我从其他来源看到它在+/-3公里的公差范围内。
我开始怀疑你是否在使用计算,但由于地球表面的曲率,这对于远距离的精度更为重要。蒂姆,你从使用边界框(矩形)开始,然后使用哈弗森公式,你会得到一个半径(圆),如果你只想让人们保持一定距离,这通常要好得多。你不陈述你的目的,但是如果你在寻找可能与你旅行一定距离的人,你可能想考虑不同形状的大都市区。如果是这样,请看:从原点(纬度/经度)开始,精确度是否会进一步降低?不知道,我的查询主要在20公里范围内。事实上,似乎变得更糟,当我搜索40公里范围时,我得到的结果是100公里。我相信我刚刚找到了答案。虽然它说我在搜索公里数,但我相信它是以英里数为单位的。我不能相信这是那么容易。谢谢你的号码!这就是为什么我问我的第一个问题-我想知道你是否在使用GCD,但它实际上与20公里的距离无关!注意:您正在使用边界框计算。因此,即使你在哈弗森公式(又名大圆距离)中有正确的半径数(以km为单位),你也会在半径之外,在边界框的拐角处得到一些命中。在人口稀少的地区,这可能没什么大不了的。但在纽约或多伦多这样的地方,这确实有点让人困惑。