Created
July 17, 2014 09:42
-
-
Save iainmullan/899f923ab6cf445f2b4a to your computer and use it in GitHub Desktop.
Rough method for calculating a geographical midpoint, given a set of lat/lng pairs.
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 | |
/** | |
* Rough method for calculating a geographical midpoint, given a set of lat/lng pairs. | |
* Assumes the earth is flat. This still gives a good approximation when deal with a small enough area. | |
* (eg. My use case is a handful of pubs in a particular part of town) | |
*/ | |
function geo_midpoint($points) { | |
$lats = array(); | |
$lngs = array(); | |
foreach($points as $p) { | |
$lats[] = $p['lat'] + 90; | |
$lngs[] = $p['lng'] + 180; | |
} | |
return array( | |
'lat' => (array_sum($lats) / count($lats)) - 90; | |
'lng' => (array_sum($lngs) / count($lngs)) - 180; | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment