PHP - Calculate the center point of multiple latitude/longitude coordinate pairs
function GetCenterFromDegrees(array $data) | |
{ | |
if (!count($data)) { | |
return false; | |
} | |
$numCoords = count($data); | |
$X = 0.0; | |
$Y = 0.0; | |
$Z = 0.0; | |
for($i = 0; $i < count($data); $i++){ | |
$lat = $data[$i][0] * pi() / 180; | |
$lon = $data[$i][1] * pi() / 180; | |
$a = cos($lat) * cos($lon); | |
$b = cos($lat) * sin($lon); | |
$c = sin($lat); | |
$X += $a; | |
$Y += $b; | |
$Z += $c; | |
} | |
$X /= $numCoords; | |
$Y /= $numCoords; | |
$Z /= $numCoords; | |
$lon = atan2($Y, $X); | |
$hyp = sqrt($X * $X + $Y * $Y); | |
$lat = atan2($Z, $hyp); | |
$newX = ($lat * 180 / pi()); | |
$newY = ($lon * 180 / pi()); | |
return [$newX, $newY]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment