This is my 2nd in a series of three articles that shows how to use trigonometry to gain useful information about the Google Earth user environment.
In this article I'm providing the code for a PHP function to calculate the
coordinates of an unknown point from a known position, range and bearing. This can be used with the
LookAt variables sent to the server by
NetworkLink to calculate the coordinates of the viewer,
especially when the viewpoint is tilted and skewed and the viewpoint is far away from the
BBOX.
My 3rd article shows a single PHP function to calculate both the location and altitude of the unknown viewpoint from just the
LookAt data, and is a combination of this function and my previously published
altitude() function.
Code:
/**
* Calculate the unknown coordinates at bearing and distance from known coordinates
*
* Developed by TJ (http://tjworld.net/software/kml/) for Google Earth applications
*
* @param float long Longitude of known point (decimal degreees)
* @param float lat Latitude of known point (decimal degrees)
* @param float distance Distance to unknown point (meters)
* @param float bearing Bearing (angle in degrees) to unknown point.
* Measured from North=0, -ve going to west (-90), +ve going to east (90)
* @returns array Associative array containing unknown 'longitude' and 'latitude'
*/
$EARTH_RADIUS_EQUATOR = 6378140.0;
$RADIAN = 180 / pi();
function calcLatLong($long, $lat, $distance, $bearing) {
global $EARTH_RADIUS_EQUATOR, $RADIAN;
$b = $bearing / $RADIAN;
$long = $long / $RADIAN;
$lat = $lat / $RADIAN;
$f = 1/298.257;
$e = 0.08181922;
$R = $EARTH_RADIUS_EQUATOR * (1 - $e * $e) / pow( (1 - $e*$e * pow(sin($lat),2)), 1.5);
$psi = $distance/$R;
$phi = pi()/2 - $lat;
$arccos = cos($psi) * cos($phi) + sin($psi) * sin($phi) * cos($b);
$latA = (pi()/2 - acos($arccos)) * $RADIAN;
$arcsin = sin($b) * sin($psi) / sin($phi);
$longA = ($long - asin($arcsin)) * $RADIAN;
return array('longitude' => $longA, 'latitude' => $latA);
}