[英]Latitude/Longitude -> X/Y, X/Y -> Latitude/Longitude
我找到了一些代碼,可以將緯度/經度轉換為給定圖像上的像素,反之亦然。 我將其移植到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
我自己嘗試過修復它,但是我在數學上苦苦掙扎。 我不太確定為什么會這樣。
你是否已經嘗試過這個 ?
所以你的代碼看起來像
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
};
};
或者,如果你不希望整個世界地圖這個
並確保您的坐標以EPSG:3857標准給出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.