[英]Latitude/Longitude -> X/Y, X/Y -> Latitude/Longitude
I found some code for converting latitude/longitude to pixels on a given image and vice versa. 我找到了一些代码,可以将纬度/经度转换为给定图像上的像素,反之亦然。 I ported it to JavaScript but am getting incorrect return values on the latitude. 我将其移植到JavaScript,但是在纬度上得到了不正确的返回值。 I tested the original, unmodified code and it gives the same inaccuracies. 我测试了原始的,未经修改的代码,它给出了同样的错误。 Below is the code with the values it returns. 下面是带有返回值的代码。
function degrees_pixels(latitude, longitude, width, height) {
return {
x: ( longitude + 180 ) * ( width / 360 ),
y: ( height / 2 ) - ( width * Math.log(Math.tan(( Math.PI / 4 ) + ( ( latitude * Math.PI / 180 ) / 2 )) ) / ( 2 * Math.PI ) )
};
};
function pixels_degrees(pixel_x, pixel_y, width, height) {
return {
latitude: ( Math.exp( -( pixel_y - ( height / 2 ) ) / width * ( 2 * Math.PI )) - Math.tan(( Math.PI / 4 )) * 2 ) / ( Math.PI / 180 ),
longitude: ( pixel_x - ( width / 2 ) ) / ( width / 360 )
};
};
var tmp1 = degrees_pixels(40.7127, -74.0059, 256, 256);
console.log(tmp1); // Prints {"x":75.3735822222222,"y":96.2511781695169} which is correct
console.log(pixels_degrees(tmp1.x, tmp1.y, 256, 256)); // Prints {"latitude":10.3018054035438,"longitude":-74.0059} of which the latitude is incorrect
I have tried fixing it myself but I am struggling with the math. 我自己尝试过修复它,但是我在数学上苦苦挣扎。 I am not really sure why it is off the way it is. 我不太确定为什么会这样。
Have you already tried this ? 你是否已经尝试过这个 ?
So your code looks something like 所以你的代码看起来像
function degrees_pixels(latitude, longitude, width, height) {
return {
x: Math.round((longitude + 180) * (width / 360));,
y: Math.round(((-1 * latitude) + 90) * (height / 180))
};
};
function pixels_degrees(pixel_x, pixel_y, width, height) {
return {
latitude: (pixel_y/(height/180)-90)/-1,
longitude: pixel_x/(width/360)-180
};
};
Or if you don't want a whole world map this 或者,如果你不希望整个世界地图这个
And make sure your coordinates are given in EPSG:3857 standard. 并确保您的坐标以EPSG:3857标准给出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.