javascript 将纬度/经度转换为像素坐标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18838915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert lat/lon to pixel coordinate?
提问by Gigamegs
I'm trying to convert a lat/lon pair to a pixel coordinate. I have found this mercator projection but I don't understand the code. What is the factor,x_adj, y_adj variable? When I run the code without those constants my lat/lon pair is not on my map and the x and y pixel coordinate is not what I want.
我正在尝试将纬度/经度对转换为像素坐标。我找到了这个墨卡托投影,但我不明白代码。什么是因子,x_adj,y_adj 变量?当我在没有这些常量的情况下运行代码时,我的纬度/经度对不在我的地图上,并且 x 和 y 像素坐标不是我想要的。
function get_xy(lat, lng)
{
var mapWidth=2058;
var mapHeight=1746;
var factor=.404;
var x_adj=-391;
var y_adj=37;
var x = (mapWidth*(180+lng)/360)%mapWidth+(mapWidth/2);
var latRad = lat*Math.PI/180;
var mercN = Math.log(Math.tan((Math.PI/4)+(latRad/2)));
var y = (mapHeight/2)-(mapWidth*mercN/(2*Math.PI));
return { x: x*factor+x_adj,y: y*factor+y_adj}
}
来源:http: //webdesignerwall.com/tutorials/interactive-world-javascript-map/comment-page-1?replytocom= 103225
[2] Covert latitude/longitude point to a pixels (x,y) on mercator projection
采纳答案by MvG
Where did those variables come from
这些变量从何而来
These variables are chosen to match the computed coordinates to the background image of the map. If the projection parameters of the map were known, they could be computed. But I believe it is far more likely that they were obtained through trial and error.
选择这些变量以将计算出的坐标与地图的背景图像相匹配。如果地图的投影参数已知,则可以计算它们。但我相信它们更有可能是通过反复试验获得的。
How to compute a Mercator projection
如何计算墨卡托投影
If you want a more general method to describe the section of the world a given (not transverse) Mercator mapshows, you can use this code:
如果您想要一种更通用的方法来描述给定(非横向)墨卡托地图显示的世界部分,您可以使用以下代码:
// 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);
}
A demo run of this code is available at http://ideone.com/05OhG6.
此代码的演示运行可从http://ideone.com/05OhG6 获得。
Regarding aspect ratio
关于纵横比
A setup with $xFactor != $yFactor
produces a kind of stretched Mercator projection. This is not conformal (angle-preserving) any more. If one wants a true Mercator projection, one can omit any of the first six variable assignments, i.e. those defining the bounding box or those describing the size of the resulting map, and then use some computation too choose it satisfying $xFactor == $yFactor
. But since the choice which to omit is kind of arbitrary, I feel that the above code is the most symmetric way to describe things.
设置$xFactor != $yFactor
产生一种拉伸墨卡托投影。这不再是共形(保角)了。如果想要真正的墨卡托投影,可以省略前六个变量赋值中的任何一个,即那些定义边界框或描述结果地图大小的赋值,然后使用一些计算也选择它满足$xFactor == $yFactor
。但是由于选择省略哪个有点随意,所以我觉得上面的代码是描述事物最对称的方式。
回答by user6730393
Here's how to get the returned variables X and Y from the function you've found...
以下是如何从您找到的函数中获取返回的变量 X 和 Y...
var xy=get_xy(56,34);
var X=xy.x;
var Y=xy.y;
Now X and Y contain the coordinates.
现在 X 和 Y 包含坐标。