Skip to content

Instantly share code, notes, and snippets.

@iainmullan
Created July 17, 2014 09:42
Show Gist options
  • Save iainmullan/899f923ab6cf445f2b4a to your computer and use it in GitHub Desktop.
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.
<?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