Why is the result different from javascript to PHP?

  • Thread starter Thread starter Nodoso
  • Start date Start date
N

Nodoso

Guest
Hello

I tried to convert a formula from javascript to PHP but the thing is I have a different result and I can't see why?

The formula is to calculate the bearing.

JavaScript:
var dLat = (lat2-lat1).toRad();
var dLon = (lon2-lon1).toRad();
var y = Math.sin(dLon) * Math.cos(lat2);
var x = Math.cos(lat1)*Math.sin(lat2) -
Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
var brng = Math.atan2(y, x).toDeg();

PHP
$lat1 = $_GET['lat1'];
$lon1 = $_GET['long1'];

$lat2 = $_GET['lat2'];
$lon2 = $_GET['long2'];


$dLat = deg2rad($lat2 - $lat1);
$dLon = deg2rad($lon2 - $lon1);


$y = sin($dLon) * cos($lat2);
$x = (cos($lat1) * sin($lat2)) - (sin($lat1) * cos($lat2) * cos($dLon));
$bearing = atan2($y, $x);
$bearing = (rad2deg($bearing));

Any help is appreciated thanks.
 
Back
Top