给出初始纬度,距离和方位的PHP中的纬度经度点

时间:2012-06-19 23:51:58

标签: php google-maps distance bearing

在php中,给定一个纬度和经度点,一个方位(以度为单位)和一个距离(以英尺或公里或其他为单位),我如何计算新的lat lng点是什么?这是我尝试过的,但这是错误的。

function destinationPoint($lat, $lng, $brng, $dist) {
      $meters = $dist/3.2808399; // dist in meters
      $dist =  $meters/1000; // dist in km
      $rad = 6371; // earths mean radius
      $dist = $dist/$rad;  // convert dist to angular distance in radians
      $brng = deg2rad($brng);  // conver to radians 
      $lat1 = deg2rad($lat); 
      $lon1 = deg2rad($lng);

      $lat2 = asin(sin($lat1)*cos($dist) + cos($lat1)*sin($dist)*cos($brng) );
      $lon2 = $lon1 + atan2(sin($brng)*sin($dist)*cos($lat1),cos($dist)-sin($lat1)*sin($lat2));
      $lon2 = ($lon2+3*M_PI) % (2*M_PI) - M_PI;  // normalise to -180..+180º
      $lat2 = rad2deg($lat2);
      $lon2 = rad2deg($lon2);


        echo "lat2 = ".$lat2."<br/>";
        echo "lon2 = ".$lon2."<br/>";
    }

2 个答案:

答案 0 :(得分:4)

只需更改

$lon2 = ($lon2+3*M_PI) % (2*M_PI) - M_PI;

$lon2 = fmod($lon2 + 3*M_PI, 2*M_PI) - M_PI;

根据PHP's documentation about the modulus operator (%)

  

在处理之前,模数的操作数被转换为整数(通过去除小数部分)。

fmod“[r] eturns参数除法的浮点余数(modulo)。”

答案 1 :(得分:0)

在初始lat lng,distance`

中查找php中的纬度经度点
    $lat = $_GET['lat'];
    $lng = $_GET['lng'];
    $dist = $_GET['dist'];

    function destinationPoint($lat = null, $lng = null, $dist = null) {

         // index.php?lat=23.733023&lng=90.398384&dist=5
         // $lat = null, $lng = null, $brng = null, $dist = null
         //$lat = 23.7545821;
         //$lng = 90.3896952;

        $brng = 360;
        $feet = ($dist * 3280.84); // 1 KM = 3280.84 feet 
        $per_meter = 3.2808399; // 1 meter = 3.2808399 feet
        $circular_geo_location = array();

        for ($i = 45; $i <= 360; $i+=45) {
            $meters = $feet / $per_meter; // dist in meters
            $dist = number_format(($meters / 1000), 6); // dist in km
            $rad = 6371; // earths mean radius
            $dist = $dist / $rad;  // convert dist to angular distance in radians
            $brng = deg2rad($i);  // conver to radians $brng
            $lat1 = deg2rad($lat);
            $lon1 = deg2rad($lng);

            $lat2 = asin(sin($lat1) * cos($dist) + cos($lat1) * sin($dist) * cos($brng));
            $lon2 = $lon1 + atan2(sin($brng) * sin($dist) * cos($lat1), cos($dist) - sin($lat1) * sin($lat2));
            $lon2 = fmod($lon2 + 3 * M_PI, 2 * M_PI) - M_PI;  // normalise to -180..+180º
            $lat2 = number_format(rad2deg($lat2), 6);
            $lon2 = number_format(rad2deg($lon2), 6);

            $circular_geo_location[] = array(
                'lat' => doubleval($lat2),
                'lng' => doubleval($lon2)
            );
        }

          echo json_encode($circular_geo_location);
    }