Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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/>";
    }
like image 762
user379468 Avatar asked Jun 19 '12 23:06

user379468


People also ask

How can I find the distance between two latitude and longitude in PHP?

php function distance($lat1, $lon1, $lat2, $lon2, $unit) { if (($lat1 == $lat2) && ($lon1 == $lon2)) { return 0; } else { $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 = $ ...

What is the latitude and longitude for my location?

On Site Using a Mobile Device Android: Open Google Maps; it will zoom to your approximate location. Press and hold on the screen to drop a pin marker. Click on the dropped pin; latitude and longitude will be displayed below the map.


1 Answers

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."

like image 69
creemama Avatar answered Oct 05 '22 05:10

creemama