Skip to content

Instantly share code, notes, and snippets.

@jwhazel
Created November 30, 2016 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwhazel/ddfed7bceb3af75d8ce7c348dddaee6f to your computer and use it in GitHub Desktop.
Save jwhazel/ddfed7bceb3af75d8ce7c348dddaee6f to your computer and use it in GitHub Desktop.
PHP function for converting web mercator (X,Y coords) to WGS84
//Convert Web Mercator to WGS84
function toGeographic($X, $Y){
if (abs($X) < 180 && abs($Y) < 90){
return array('lat'=>0,'lng'=>0);
}
if (abs($X) > 20037508.3427892 || abs($Y) > 20037508.3427892){
return array('lat'=>0,'lng'=>0);
}
$num3 = ($X / 6378137.0) * 57.295779513082323;
$num4 = floor(($num3 + 180.0) / 360.0);
$num5 = $num3 - ($num4 * 360.0);
$num6 = 1.5707963267948966 - (2.0 * atan(exp((-1.0 * $Y) / 6378137.0)));
//Return array(lat,lng);
return ['lat'=>$num6 * 57.295779513082323, 'lng'=>$num5];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment