用Haversine公式计算PHP中两个邮政编码之间的距离
我试图使用php中的Haversine公式计算两个邮政编码之间的距离;到目前为止,我已经创建了一个函数来计算距离。我的数据库表名为“postcode”,字段为“postcode_id”,“postcode”,“lat”,“lng”用Haversine公式计算PHP中两个邮政编码之间的距离,php,mysql,distance,latitude-longitude,Php,Mysql,Distance,Latitude Longitude,我试图使用php中的Haversine公式计算两个邮政编码之间的距离;到目前为止,我已经创建了一个函数来计算距离。我的数据库表名为“postcode”,字段为“postcode_id”,“postcode”,“lat”,“lng” <script> function getDistance($latitude1, $longitude1, $latitude2, $longitude2) { $earth_radius = 6371; $postLa
<script>
function getDistance($latitude1, $longitude1, $latitude2, $longitude2) {
$earth_radius = 6371;
$postLat = deg2rad($latitude2 - $latitude1);
$postLon = deg2rad($longitude2 - $longitude1);
$a = sin($postLat/2) * sin($postLat/2) + cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * sin($postLon/2) * sin($postLon/2);
$c = 2 * asin(sqrt($a));
$d = $earth_radius * $c;
return $d;
}
</script>
<?php
$postcode_qry = mysqli_query($connect, "SELECT *,(((acos(sin((".$postLat."*pi()/180)) * sin(('lat'*pi()/180))+cos((".$postLat."*pi()/180)) * cos(('lat'*pi()/180)) * cos(((".$postLon."- 'lng')*pi()/180))))*180/pi())*60*1.1515) as distance
FROM `postcode` WHERE distance = ".$d."");
?>
函数getDistance($latitude1,$longitude1,$latitude2,$longitude2){
$earth_半径=6371;
$postLat=deg2rad($latude2-$latude1);
$postLon=deg2rad($longitude2-$longitude1);
$a=sin($postLat/2)*sin($postLat/2)+cos(deg2rad($latude1))*cos(deg2rad($latude2))*sin($postLon/2)*sin($postLon/2);
$c=2*asin(sqrt($a));
$d=$earth\u半径*$c;
返回$d;
}
您可以通过谷歌服务申请:
function getLongLat($address) {
$url = 'http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&language=nl&address='+$address;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$obj = simplexml_load_string($response);
if ($obj) {
$obj = $obj->result;
return array( 'latitude' => $obj->geometry->location->lat.'', 'longitude' => $obj->geometry->location->lng.'',);
}else{
return [];
}
}
谷歌将尝试为您解析地址,猜测邮政编码、国家/地区
应该是您的地址,然后您可以将所需数据传递给您的MySQL
函数,但有这么多示例@草莓请你详细说明一下,或者给我一个链接到这些例子中的一个。在这个论坛上搜索“哈弗森”或“经度”。或者点击“经纬度”标签。它没有,但是如果你阅读OP问题的内容,你会发现他已经有了数学知识,但是缺少了他需要的坐标,计算了两个地方之间的距离。因此,我在我的答案的最后评论…他也,即使