Created
February 2, 2015 14:42
-
-
Save netsensei/58d2b145f68aa678e902 to your computer and use it in GitHub Desktop.
Adjust coordinates for Google Maps via pixel offsets. Ideal for offsetting the center of your map based on a marker position.
This file contains 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
/** | |
* Class GoogleMapAdjust | |
* | |
* Adjusts latitude & longitude coördinates by pixel offsets. | |
* | |
* This class takes a coordinate, converts it to a pixel value, calculates the | |
* offsetted value and converts back to the coordinate. | |
* | |
* Primary use case: offset the center of a Google Map canvas from a marker | |
* position on your map. This is useful if you don't want markers to be shown | |
* dead center because you want to display extra information in a (fixed) | |
* position near the marker. | |
* | |
* @see | |
* https://developers.google.com/maps/documentation/staticmaps/ | |
* | |
* @see | |
* http://stackoverflow.com/questions/1763917/google-static-maps-move-maps-with-a-finger | |
*/ | |
class GoogleMapAdjust { | |
const GOOGLEOFFSET = 268435456; | |
protected $googleOffsetRadius; | |
protected $mathPi180; | |
protected $preLonToX1; | |
public function __construct() { | |
$this->googleOffsetRadius = GoogleMapAdjust::GOOGLEOFFSET / M_PI; | |
$this->mathPi180 = M_PI / 180; | |
$this->preLonToX1 = $this->googleOffsetRadius * (M_PI / 180); | |
} | |
/** | |
* Convert a longitude coordinate to a pixel value | |
*/ | |
private function LonToX($lon) { | |
return round(GoogleMapAdjust::GOOGLEOFFSET + $this->preLonToX1 * $lon); | |
} | |
/** | |
* Convert a latitude coordinate to a pixel value | |
*/ | |
private function LatToY($lat) { | |
return round(GoogleMapAdjust::GOOGLEOFFSET - $this->googleOffsetRadius * log((1 + sin($lat * $this->mathPi180)) / (1 - sin($lat * $this->mathPi180))) / 2); | |
} | |
/** | |
* Converts a longitude pixel value to a coordinate | |
*/ | |
private function XToLon($x) { | |
return ((round($x) - GoogleMapAdjust::GOOGLEOFFSET) / $this->googleOffsetRadius) * 180 / M_PI; | |
} | |
/** | |
* Converts a latitude pixel value to a coordinate | |
*/ | |
private function YToLat($y) { | |
return (M_PI / 2 - 2 * atan(exp((round($y) - GoogleMapAdjust::GOOGLEOFFSET) / $this->googleOffsetRadius))) * 180 / M_PI; | |
} | |
/** | |
* Adjust the longitude of a coördinate on a map by a given number of pixels. | |
* | |
* @param $lon | |
* The longitude ie. 4.22345 | |
* @param $delta | |
* The offset in pixels. Can be positive or negative | |
* @param $zoom | |
* The zoom level of the map between 0..21 | |
* @return float | |
* The re-calculated longitude coordinate | |
*/ | |
public function adjustLonByPixels($lon, $delta, $zoom) { | |
$x = $this->LonToX($lon) + ($delta << (21 - $zoom)); | |
return $this->XToLon($x); | |
} | |
/** | |
* Adjust the latitude of a coördinate on a map by a given number of pixels. | |
* | |
* @param $lat | |
* The latitude ie 52.2345 | |
* @param $delta | |
* The offset in pixels. Can be positive or negative | |
* @param $zoom | |
* The zoom level of the map between 0..21 | |
* @return float | |
* The re-calculated latitude coordinate | |
*/ | |
public function adjustLatByPixels($lat, $delta, $zoom) { | |
$y = $this->LatToY($lat) + ($delta << (21 - $zoom)); | |
return $this->YToLat($y); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment