Created
May 17, 2017 05:49
-
-
Save marcus-at-localhost/318579448711e9155e4b706bdf4a694b to your computer and use it in GitHub Desktop.
Find a center point of multiple lat lng on Google Maps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Find a center point of multiple lat lng on Google Maps | |
// $data = [ | |
// 0 => [-8.412942,115.238952], | |
// 1 => [-8.800293,115.160986] | |
// ]; | |
function GetCenterFromDegrees($data) | |
{ | |
if (!is_array($data)) return FALSE; | |
$num_coords = count($data); | |
$X = 0.0; | |
$Y = 0.0; | |
$Z = 0.0; | |
foreach ($data as $coord) | |
{ | |
$lat = $coord[0] * pi() / 180; | |
$lon = $coord[1] * pi() / 180; | |
$a = cos($lat) * cos($lon); | |
$b = cos($lat) * sin($lon); | |
$c = sin($lat); | |
$X += $a; | |
$Y += $b; | |
$Z += $c; | |
} | |
$X /= $num_coords; | |
$Y /= $num_coords; | |
$Z /= $num_coords; | |
$lon = atan2($Y, $X); | |
$hyp = sqrt($X * $X + $Y * $Y); | |
$lat = atan2($Z, $hyp); | |
return array($lat * 180 / pi(), $lon * 180 / pi()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment