Skip to content

Instantly share code, notes, and snippets.

@safecat
Last active August 29, 2015 14:01
Show Gist options
  • Save safecat/d43818438cb6b20dc2e6 to your computer and use it in GitHub Desktop.
Save safecat/d43818438cb6b20dc2e6 to your computer and use it in GitHub Desktop.
PHP Mercator Projection
<?php
//based on http://www.cnblogs.com/enjoyeclipse/archive/2013/01/18/2865700.html
function latlng2xy($lat, $lng, $zoom){
if (abs($lat) > 85.0511287798066){
return false;
}
$sin_phi = sin($lat * M_PI / 180);
$norm_x = $lng / 180;
$norm_y = (0.5 * log((1 + $sin_phi) / (1 - $sin_phi))) / M_PI;
$tileRow = pow(2, $zoom) * ((1 - $norm_y) / 2);
$tileColumn = pow(2, $zoom) * (($norm_x + 1) / 2);
return array(
'y'=>$tileRow,
'x'=>$tileColumn
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment