将纬度/经度转换为像素坐标? [英] Convert lat/lon to pixel coordinate?

查看:741
本文介绍了将纬度/经度转换为像素坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将经纬度对转换为像素坐标。我发现了这个mercator投影,但我不明白代码。什么是因素,x_adj,y_adj变量?
当我运行没有这些常量的代码时,我的纬度/经度对不在我的地图上,x和y像素坐标不是我想要的。

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.

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] 隐蔽的经度/纬度指向一个像素( x,y)on mercator projection

推荐答案

这些变量来自何处



选择这些变量以将计算出的坐标与地图的背景图像进行匹配。如果地图的投影参数是已知的,则可以计算它们。但我相信它更有可能是通过反复试验获得的。

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.

如果你想要一个更一般的方法来描述世界的某个部分(不是横向的)墨卡托地图显示,您可以使用以下代码:

If you want a more general method to describe the section of the world a given (not transverse) Mercator map shows, 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);
}

此代码的演示可在 http://ideone.com/05OhG6

使用 $ xFactor!= $ yFactor 的设置会产生一种拉伸的墨卡托投影。这不再是保形(角度保持)。如果想要一个真正的墨卡托投影,可以忽略前六个变量赋值中的任何一个,即定义边界框或描述结果映射大小的变量赋值,然后使用一些计算选择满足 $ xFactor == $ yFactor 。但是,由于要省略的选择是任意的,所以我觉得上面的代码是描述事物的最对称的方式。

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.

这篇关于将纬度/经度转换为像素坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆