Skip to content

Instantly share code, notes, and snippets.

@seblambla
Last active August 29, 2015 14:15
Show Gist options
  • Save seblambla/6d526e8704f10bb7d71d to your computer and use it in GitHub Desktop.
Save seblambla/6d526e8704f10bb7d71d to your computer and use it in GitHub Desktop.
Geographic coordinates to 2D x% and y% coords
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