我想计算两个纬度和经度之间的距离,如果输出距离小于km,则显示距离以米为单位,如谷歌地图"190米";;截至目前,以英里为单位显示
<?php
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if($miles <1km){
echo 'away from'.$miles .'meters';// distance in meters
}
else{
echo 'away from'$miles.'km';
}
}
echo distance('17.360527','78.473542','17.361012','78.473062','m');
?>
我认为这对你有用:
<?php
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
// Convert Miles To KM
$dis_in_km = $miles * 1.609344;
if($dis_in_km < 1){
// Convert Miles to Miters
$dis_in_m = $miles * 1609.344;
echo 'away from '. round($dis_in_m,2).' meters';
}else{
echo 'away from '.round($dis_in_km,2).' km';
}
}
echo distance('17.360527','78.473542','17.361012','78.473062','m');
?>