Find midpoint between two points

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux