FInd latitude longitude point in php given initial lat lng, distance, and bearing

爱⌒轻易说出口 提交于 2019-12-07 03:35:46

问题


In php, given a latitude and longitude point, a bearing (in degrees), and a distance (in feet or km or whatever) how do I figure out what the new lat lng point is? here is what I tried, but it's wrong.

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/>";
    }

回答1:


Just change

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

to

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

According to PHP's documentation about the modulus operator (%),

Operands of modulus are converted to integers (by stripping the decimal part) before processing.

fmod "[r]eturns the floating point remainder (modulo) of the division of the arguments."




回答2:


Find latitude longitude point in php given initial lat lng, distance`

    $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);
    }


来源:https://stackoverflow.com/questions/11111077/find-latitude-longitude-point-in-php-given-initial-lat-lng-distance-and-bearin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!