gpt4 book ai didi

php - 根据php中的一个邮政编码找到最近的邮政编码

转载 作者:搜寻专家 更新时间:2023-10-31 21:52:54 25 4
gpt4 key购买 nike

我有一个包含所有州邮政编码的数据库。我在我的数据库中配置了它,字段是邮政编码、纬度、日志。如果我在我的表格中提供一个邮政编码并提交,那么我必须通过指定有限的公里数来获得最近的邮政编码。是否有任何 php 代码可以执行此操作?请帮助我吗?

我找到了一些代码来获取距离

<?php
function getDistance($latitude1, $longitude1, $latitude2, $longitude2) {
$earth_radius = 6371;

$dLat = deg2rad($latitude2 - $latitude1);
$dLon = deg2rad($longitude2 - $longitude1);

$a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * sin($dLon/2) * sin($dLon/2);
$c = 2 * asin(sqrt($a));
$d = $earth_radius * $c;

return $d;
}

?>

像这样如何根据一个邮政编码找到最近的邮政编码。

最佳答案

您可以使用 sql 查询来选择距离内的所有邮政编码 ##

    $distance=$_POST['specified_distance'];
$search_postcode=$_POST['specified_postcode'];
//use search postcode to get the row form database assume it to be searchrow
$latitude=$searchrow['lat'];
$longitude=$searchrow['long'];
$sql = "SELECT *,(((acos(sin((".$latitude."*pi()/180)) * sin((`lat`*pi()/180))+cos((".$latitude."*pi()/180))
* cos((`lat`*pi()/180)) * cos(((".$longitude."- `long`)*pi()/180))))*180/pi())*60*1.1515*1.609344)
as distance
FROM `MyTable`
WHERE distance <= ".$distance;

$result = $conn->query($sql);

if ($result->num_rows > 0) {
$postcode_rows=array();
while($row = $result->fetch_assoc()) {
$postcode_rows[$row['distance']]=$row; //get each row to associative //array with distance as key
}
//do array krsort to get the least distance row;
$sorted_postcodes_rows=krsort($postcodes);
$selected_postcode_row=array_shift($sorted_postcodes); //get the nearest post //code
echo =$nearest_postcode=$selected_postcode_row['postcode'];
} else {
echo "No nearest postalcode";
}

关于php - 根据php中的一个邮政编码找到最近的邮政编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37936072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com