我正在寻找一种优雅的解决方案来计算center
几个经纬度坐标之间的坐标(例如,简单地将地图居中到谷歌地图多边形的中心)。
表locations
::
id | city | latitude | longitude
-----------------------------------------------
1 | Berlin | 52.524268 | 13.406290
-----------------------------------------------
2 | London | 51.508129 | -0.1280050
-----------------------------------------------
3 | Hamburg | 53.551084 | 9.9936817
-----------------------------------------------
4 | Amsterdam | 52.370215 | 4.8951678
-----------------------------------------------
当前计算:
function calculateCenter($array_locations) {
$minlat = false;
$minlng = false;
$maxlat = false;
$maxlng = false;
foreach ($array_locations as $geolocation) {
if ($minlat === false) { $minlat = $geolocation['lat']; } else { $minlat = ($geolocation['lat'] < $minlat) ? $geolocation['lat'] : $minlat; }
if ($maxlat === false) { $maxlat = $geolocation['lat']; } else { $maxlat = ($geolocation['lat'] > $maxlat) ? $geolocation['lat'] : $maxlat; }
if ($minlng === false) { $minlng = $geolocation['lon']; } else { $minlng = ($geolocation['lon'] < $minlng) ? $geolocation['lon'] : $minlng; }
if ($maxlng === false) { $maxlng = $geolocation['lon']; } else { $maxlng = ($geolocation['lon'] > $maxlng) ? $geolocation['lon'] : $maxlng; }
}
// Calculate the center
$lat = $maxlat - (($maxlat - $minlat) / 2);
$lon = $maxlng - (($maxlng - $minlng) / 2);
return array($lat, $lon);
}