Last active
August 29, 2015 14:15
-
-
Save seblambla/6d526e8704f10bb7d71d to your computer and use it in GitHub Desktop.
Geographic coordinates to 2D x% and y% coords
This file contains hidden or 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
function getCoordinates($lat, $lng){ | |
$mapWidth = 1000; // change this value | |
$mapHeight = 1000; // change this value | |
$pi = pi(); | |
// get x value | |
$c['x'] = ($lng+180)*($mapWidth/360); | |
// convert from degrees to radians | |
$latRad = $lat*$pi/180; | |
// get y value | |
$mercN = log(tan(($pi/4)+($latRad/2))); | |
$c['y'] = ($mapHeight/2)-($mapWidth*$mercN/(2*$pi)); | |
// x and y to x% and y% | |
$c['x'] = (100*$c['x'])/$mapWidth; | |
$c['y'] = (100*$c['y'])/$mapHeight; | |
return $c; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment