这些变量从何而来
选择这些变量以使计算出的坐标与地图的背景图像匹配。如果知道地图的投影参数,则可以计算它们。但是我相信,它们很可能是通过反复试验而获得的。
如何计算墨卡托投影
如果要使用更通用的方法来描述给定(而非横向)墨卡托地图]显示的世界部分,则可以使用以下代码:
// This map would show Germany:$south = deg2rad(47.2);$north = deg2rad(55.2);$west = deg2rad(5.8);$east = deg2rad(15.2);// This also controls the aspect ratio of the projection$width = 1000;$height = 1500;// Formula for mercator projection y coordinate:function mercY($lat) { return log(tan($lat/2 + M_PI/4)); }// Some constants to relate chosen area to screen coordinates$ymin = mercY($south);$ymax = mercY($north);$xFactor = $width/($east - $west);$yFactor = $height/($ymax - $ymin);function mapProject($lat, $lon) { // both in radians, use deg2rad if neccessary global $xFactor, $yFactor, $west, $ymax; $x = $lon; $y = mercY($lat); $x = ($x - $west)*$xFactor; $y = ($ymax - $y)*$yFactor; // y points south return array($x, $y);}关于宽高比
的设置
$xFactor !=$yFactor会产生一种拉伸的墨卡托投影。这不再是保形的(保持角度)。如果想要一个真正的墨卡托投影,则可以忽略前六个变量分配中的任何一个,即定义边界框的那些或描述结果贴图的大小的那些,然后再使用一些计算选择令人满意的值
$xFactor==$yFactor。但是由于忽略的选择是任意的,因此我认为上述代码是描述事物的最对称方式。



