简体   繁体   English

将lat / lon转换为像素坐标?

[英]Convert lat/lon to pixel coordinate?

I'm trying to convert a lat/lon pair to a pixel coordinate. 我正在尝试将lat / lon对转换为像素坐标。 I have found this mercator projection but I don't understand the code. 我找到了这个墨卡托投影,但我不明白代码。 What is the factor,x_adj, y_adj variable? x_adj,y_adj变量的因素是什么? 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. 当我运行没有这些常量的代码时,我的lat / lon对不在我的地图上,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}
}

Source: http://webdesignerwall.com/tutorials/interactive-world-javascript-map/comment-page-1?replytocom=103225 资料来源: 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 [2] 隐蔽纬度/经度指向墨卡托投影上的像素(x,y)

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 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);
}

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. 使用$xFactor != $yFactor会产生一种拉伸的墨卡托投影。 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, ie 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 . 如果想要一个真正的墨卡托投影,可以省略前六个变量赋值中的任何一个,即定义边界框的那些或描述结果映射大小的那些,然后使用一些计算也选择它满足$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. 但由于省略的选择有点随意,我觉得上面的代码是描述事物的最对称的方式。

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包含坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM