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

时间:2016-03-15 01:43:56

标签: javascript latitude-longitude mercator

我找到了一些代码,用于将纬度/经度转换为给定图像上的像素,反之亦然。我将它移植到JavaScript但是在纬度上获得了不正确的返回值。我测试了原始的,未经修改的代码,它给出了相同的不准确性。下面是包含它返回值的代码。

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

我自己试过修理它,但我在努力学习数学。我不确定为什么它会偏离它。

1 个答案:

答案 0 :(得分:1)

您是否已尝试this

所以你的代码看起来像

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

或者,如果您不想要整个世界地图this

并确保您的坐标以EPSG:3857标准给出。

相关问题