Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is a line by line translation of Marcelo's code in PHP, which can probably be cleaned up a bit. Works great! Thanks to Marcelo for doing the hard part.</p> <pre><code>define("MERCATOR_RANGE", 256); function degreesToRadians($deg) { return $deg * (M_PI / 180); } function radiansToDegrees($rad) { return $rad / (M_PI / 180); } function bound($value, $opt_min, $opt_max) { if ($opt_min != null) $value = max($value, $opt_min); if ($opt_max != null) $value = min($value, $opt_max); return $value; } class G_Point { public $x,$y; function G_Point($x=0, $y=0){ $this-&gt;x = $x; $this-&gt;y = $y; } } class G_LatLng { public $lat,$lng; function G_LatLng($lt, $ln){ $this-&gt;lat = $lt; $this-&gt;lng = $ln; } } class MercatorProjection { private $pixelOrigin_, $pixelsPerLonDegree_, $pixelsPerLonRadian_; function MercatorProjection() { $this-&gt;pixelOrigin_ = new G_Point( MERCATOR_RANGE / 2, MERCATOR_RANGE / 2); $this-&gt;pixelsPerLonDegree_ = MERCATOR_RANGE / 360; $this-&gt;pixelsPerLonRadian_ = MERCATOR_RANGE / (2 * M_PI); } public function fromLatLngToPoint($latLng, $opt_point=null) { $me = $this; $point = $opt_point ? $opt_point : new G_Point(0,0); $origin = $me-&gt;pixelOrigin_; $point-&gt;x = $origin-&gt;x + $latLng-&gt;lng * $me-&gt;pixelsPerLonDegree_; // NOTE(appleton): Truncating to 0.9999 effectively limits latitude to // 89.189. This is about a third of a tile past the edge of the world tile. $siny = bound(sin(degreesToRadians($latLng-&gt;lat)), -0.9999, 0.9999); $point-&gt;y = $origin-&gt;y + 0.5 * log((1 + $siny) / (1 - $siny)) * -$me-&gt;pixelsPerLonRadian_; return $point; } public function fromPointToLatLng($point) { $me = $this; $origin = $me-&gt;pixelOrigin_; $lng = ($point-&gt;x - $origin-&gt;x) / $me-&gt;pixelsPerLonDegree_; $latRadians = ($point-&gt;y - $origin-&gt;y) / -$me-&gt;pixelsPerLonRadian_; $lat = radiansToDegrees(2 * atan(exp($latRadians)) - M_PI / 2); return new G_LatLng($lat, $lng); } //pixelCoordinate = worldCoordinate * pow(2,zoomLevel) } function getCorners($center, $zoom, $mapWidth, $mapHeight){ $scale = pow(2, $zoom); $proj = new MercatorProjection(); $centerPx = $proj-&gt;fromLatLngToPoint($center); $SWPoint = new G_Point($centerPx-&gt;x-($mapWidth/2)/$scale, $centerPx-&gt;y+($mapHeight/2)/$scale); $SWLatLon = $proj-&gt;fromPointToLatLng($SWPoint); $NEPoint = new G_Point($centerPx-&gt;x+($mapWidth/2)/$scale, $centerPx-&gt;y-($mapHeight/2)/$scale); $NELatLon = $proj-&gt;fromPointToLatLng($NEPoint); return array( 'N' =&gt; $NELatLon-&gt;lat, 'E' =&gt; $NELatLon-&gt;lng, 'S' =&gt; $SWLatLon-&gt;lat, 'W' =&gt; $SWLatLon-&gt;lng, ); } </code></pre> <p>Usage:</p> <pre><code>$centerLat = 49.141404; $centerLon = -121.960988; $zoom = 10; $mapWidth = 640; $mapHeight = 640; $centerPoint = new G_LatLng($centerLat, $centerLon); $corners = getCorners($centerPoint, $zoom, $mapWidth, $mapHeight); $mapURL = "http://maps.googleapis.com/maps/api/staticmap?center={$centerLat},{$centerLon}&amp;zoom={$zoom}&amp;size={$mapWidth}x{$mapHeight}&amp;scale=2&amp;maptype=roadmap&amp;sensor=false"; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload