Skip to content

Instantly share code, notes, and snippets.

@OHIHIYA20
Forked from davidlevy/convertGeoToPixel.php
Created March 6, 2019 07:03
Show Gist options
  • Select an option

  • Save OHIHIYA20/0358c6d7855e9d67987cda0788da34bd to your computer and use it in GitHub Desktop.

Select an option

Save OHIHIYA20/0358c6d7855e9d67987cda0788da34bd to your computer and use it in GitHub Desktop.
Function to convert Geo coordinates To Pixels
<?php
function convertGeoToPixel($lat, $lon){
$mapWidth = 400;
$mapHeight = 260;
$mapLonLeft = -180;
$mapLonRight = 180;
$mapLonDelta = $mapLonRight - $mapLonLeft;
//http://stackoverflow.com/questions/2103924/mercator-longitude-and-latitude-calculations-to-x-and-y-on-a-cropped-map-of-the/10401734#10401734
//-169.4531,-44.8403,177.8906,77.9157
//-180,-56.1700,180,83.6769
$mapLatBottom = -56.1700;
$mapLatBottomDegree = $mapLatBottom * M_PI / 180;
// global $mapWidth, $mapHeight, $mapLonLeft, $mapLonDelta, $mapLatBottom, $mapLatBottomDegree;
$x = ($lon - $mapLonLeft) * ($mapWidth / $mapLonDelta);
$lat = $lat * M_PI / 180;
$worldMapWidth = (($mapWidth / $mapLonDelta) * 360) / (2 * M_PI);
$mapOffsetY = ($worldMapWidth / 2 * log((1 + sin($mapLatBottomDegree)) / (1 - sin($mapLatBottomDegree))));
$y = $mapHeight - (($worldMapWidth / 2 * log((1 + sin($lat)) / (1 - sin($lat)))) - $mapOffsetY);
return array($x, $y);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment