Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marcus-at-localhost/318579448711e9155e4b706bdf4a694b to your computer and use it in GitHub Desktop.
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
<?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