Find midpoint between two points

From: Date: Thu, 08 Feb 2007 00:52:25 +0000
Subject: Find midpoint between two points
Groups: php.general 
Request: Send a blank email to php-general+get-248523@lists.php.net to get a copy of this message
I found a nice javascript function that takes two points of latitude and longitude and returns a midpoint. I'm now trying to rewrite in PHP, but having some problems. Here's the original javascript function, taken from http://www.movable-type.co.uk/scripts/LatLong.html : LatLong.midPoint = function(p1, p2) { var dLon = p2.lon - p1.lon; var Bx = Math.cos(p2.lat) * Math.cos(dLon); var By = Math.cos(p2.lat) * Math.sin(dLon); lat3 = Math.atan2(Math.sin(p1.lat)+Math.sin(p2.lat),
                    Math.sqrt((Math.cos(p1.lat)+Bx)*(Math.cos(p1.lat)+Bx) + By*By ) );
lon3 = p1.lon + Math.atan2(By, Math.cos(p1.lat) + Bx); if (isNaN(lat3) || isNaN(lon3)) return null; return new LatLong(lat3*180/Math.PI, lon3*180/Math.PI); } And here's my PHP variant, which isn't working: function midpoint ($lat1, $lng1, $lat2, $lng2) { $dlng = $lng2 - $lng1; $Bx = cos($lat2) * cos($dlng); $By = cos($lat2) * sin($dlng); $lat3 = atan2( sin($lat1)+sin($lat2), sqrt((cos($lat1)+$Bx)*(cos($lat1)+$Bx) + $By*$By )); $lng3 = $lng1 + atan2($By, (cos($lat1) + $Bx)); $pi = pi(); return ($lat3*180)/$pi .' '. ($lng3*180)/$pi; } Any ideas why it's returning wrong values? ...Rene

« previous php.general (#248523) next »