Skip to content

Instantly share code, notes, and snippets.

@andreabazerla
Last active January 31, 2017 22:46
Show Gist options
  • Save andreabazerla/8ccc98c88c6139384a30c2e5acde7a27 to your computer and use it in GitHub Desktop.
Save andreabazerla/8ccc98c88c6139384a30c2e5acde7a27 to your computer and use it in GitHub Desktop.
http://itsvr.altervista.org/ Script in PHP for your website to plot a GPS position on a custom map converting latitude and longitude from degrees to pixels.
<?php
// Demo: http://itsvr.altervista.org/
/* Square area edges in degrees of your custom area map (Verona, Italy) */
$mapLatTop = 45.47;
$mapLatBottom = 45.37;
$mapLonLeft = 10.92;
$mapLonRight = 11.06;
$mapLatDelta = $mapLatTop - $mapLatBottom;
$mapLonDelta = $mapLonRight - $mapLonLeft;
$worldMapWidth = 180/($mapLonDelta*M_PI);
$f = sin($mapLatBottom*(M_PI/180));
$mapOffsetY = $worldMapWidth/2*log((1+$f)/(1-$f));
$f = sin($mapLatTop*(M_PI/180));
$mapOffsetTopY = $worldMapWidth/2*log((1+$f)/(1-$f));
$mapHeightNew = $mapOffsetTopY-$mapOffsetY;
$mapRatioHeight = 1/$mapHeightNew;
$mapRatioWidth = 1/$mapHeightNew;
/**
* deg_to_px($lat, $lon)
*
* Convert a GPS position on your custom map converting latitude and longitude from degrees to pixels
*
* @param (decimal(10,8)) $lat Latitude
* @param (decimal(10,8)) $lon Longitude
* @return (array()) array($x,$y) X and Y position in pixels
*/
function deg_to_px($lat, $lon) {
global $mapLonLeft, $mapLonDelta, $mapRatioWidth, $mapHeightNew, $worldMapWidth, $mapOffsetY;
$x = ($lon-$mapLonLeft)*($mapRatioWidth/$mapLonDelta);
$f = sin($lat*M_PI/180);
$y = ($mapHeightNew-(($worldMapWidth/2*log((1+$f)/(1-$f)))-$mapOffsetY));
return array($x,$y);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment