Created
November 30, 2016 16:01
-
-
Save jwhazel/ddfed7bceb3af75d8ce7c348dddaee6f to your computer and use it in GitHub Desktop.
PHP function for converting web mercator (X,Y coords) to WGS84
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
//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