zoukankan      html  css  js  c++  java
  • php 计算两点地理坐标的距离

    <?php
    /**
     * 计算两点地理坐标之间的距离
     * @param  Decimal $longitude1 起点经度
     * @param  Decimal $latitude1  起点纬度
     * @param  Decimal $longitude2 终点经度 
     * @param  Decimal $latitude2  终点纬度
     * @param  Int     $unit       单位 1:米 2:公里
     * @param  Int     $decimal    精度 保留小数位数
     * @return Decimal
     */
    function getDistance($longitude1, $latitude1, $longitude2, $latitude2, $unit=2, $decimal=2){
     
        $EARTH_RADIUS = 6370.996; // 地球半径系数
        $PI = 3.1415926;
     
        $radLat1 = $latitude1 * $PI / 180.0;
        $radLat2 = $latitude2 * $PI / 180.0;
     
        $radLng1 = $longitude1 * $PI / 180.0;
        $radLng2 = $longitude2 * $PI /180.0;
     
        $a = $radLat1 - $radLat2;
        $b = $radLng1 - $radLng2;
     
        $distance = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
        $distance = $distance * $EARTH_RADIUS * 1000;
     
        if($unit==2){
            $distance = $distance / 1000;
        }
     
        return round($distance, $decimal);
     
    }
     
    // 起点坐标
    $longitude1 = 113.330405;
    $latitude1 = 23.147255;
     
    // 终点坐标
    $longitude2 = 113.314271;
    $latitude2 = 23.1323;
     
    $distance = getDistance($longitude1, $latitude1, $longitude2, $latitude2, 1);
    echo $distance.'m'; // 2342.38m
     
    $distance = getDistance($longitude1, $latitude1, $longitude2, $latitude2, 2);
    echo $distance.'km'; // 2.34km
     
    ?>
    

      功能:根据圆周率和地球半径系数与两点坐标的经纬度,计算两点之间的球面距离。 


    获取两点坐标距离:

  • 相关阅读:
    使用60赫兹交流电的关西人
    UI交互细节节选控件使用细则
    PNG button (按钮) files with transparency, for Visual C++ 6.0 and VS2005
    成都第二天:美食
    精力管理与状态转换
    再议“专注”
    成都第一天:印象
    Flash AMF协议
    AS3.0 JSON介绍
    AS3.0 利用AMFPHP与PHP进行通讯 .
  • 原文地址:https://www.cnblogs.com/lxwphp/p/15453399.html
Copyright © 2011-2022 走看看