简体   繁体   English

纬度/经度-> X / Y,X / Y->纬度/经度

[英]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.

相关问题 从X和Y坐标获取经度和纬度 - Get latitude and longitude from X and Y coordinates 将纬度和经度转换为网页上的 x,y 网格 - Translating latitude and longitude into x,y grid on webpage 将 X,Y 像素转换为经度和纬度 - Convert X,Y pixel to Longitude and Latitude 墨卡托经纬度计算(英国)裁剪地图上的 x 和 y - Mercator longitude and latitude calculations to x and y on a cropped map (of the UK) 如何将向量(X,Y)位置转换为纬度/经度坐标? 使用Javascript - How to convert a vector (X,Y) position to a latitude/longitude coordinate? Javascript 给定地图图像,将x和y坐标转换为纬度和经度 - Given an image of a map convert an x and y coordinate to Latitude and Longitude 如何将 x、y、z 转换为 Cesium 中的经度、纬度、高度? - How to convert x, y, z to longitude, latitude, altitude in Cesium? google map 从像素 x,y 获取纬度、经度 - google map get latitude, longitude from pixel x,y 将(旋转的)ImageOverlay中的X和Y坐标转换为纬度和经度 - Transform X and Y coordinates inside a (rotated) ImageOverlay to latitude and longitude 根据半径,x和y 3D地球仪THREE.js确定3D经度和纬度 - Determine 3D Longitude and Latitude based on radius, x and y 3D globe THREE.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM