Rootop 服务器运维与web架构

php计算两个坐标距离

<?php

function distance($lng1, $lat1, $lng2, $lat2, $len_type = 1, $decimal = 2)
{
    $radLat1 = $lat1 * PI() / 180.0;
    $radLat2 = $lat2 * PI() / 180.0;
    $a = $radLat1 - $radLat2;
    $b = ($lng1 * PI() / 180.0) - ($lng2 * PI() / 180.0);
    $s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
    $s = $s * 6378.137;
    $s = round($s * 1000);

    if ($len_type == 1)  // 1 kilometer / 2 meter
    {
       $s /= 1000;
    }

    return round($s, $decimal);
}

用法:distance(坐标1经度,坐标1纬度,坐标2经度,坐标2纬度,[可选,1/2,千米/米],[精确小数位,默认2])

如:echo distance(120.389014,36.073233,116.381272,39.914271);

553.44 // 千米

原创文章,转载请注明。本文链接地址: https://www.rootop.org/pages/3625.html

作者:Venus

服务器运维与性能优化

评论已关闭。