代码之家  ›  专栏  ›  技术社区  ›  donohoe

将横向/纵向坐标转换为X/Y坐标

  •  17
  • donohoe  · 技术社区  · 17 年前

    我拥有纽约市的Lat/Long值;40.7560540,-73.9869510和地球的平面图像,1000px 446px。

    因此,图像左上角的X,Y坐标为;289, 111

    注意事项:

    1. 假设还是按照你所知道的去做 可能有用
    2. 十、 Y可以形成图像的任何一个角
    3. 奖励积分 在PHP中使用相同的解决方案(但我 真的需要JS)
    5 回复  |  直到 15 年前
        1
  •  19
  •   Community Mohan Dere    14 年前

    您使用的投影将改变一切,但这将在假设墨卡托投影的情况下起作用:

    <html>
    <head>
    <script language="Javascript">
    var dot_size = 3;
    var longitude_shift = 55;   // number of pixels your map's prime meridian is off-center.
    var x_pos = 54;
    var y_pos = 19;
    var map_width = 430;
    var map_height = 332;
    var half_dot = Math.floor(dot_size / 2);
    function draw_point(x, y) {
        dot = '<div style="position:absolute;width:' + dot_size + 'px;height:' + dot_size + 'px;top:' + y + 'px;left:' + x + 'px;background:#00ff00"></div>';
        document.body.innerHTML += dot;
    }
    function plot_point(lat, lng) {
        // Mercator projection
    
        // longitude: just scale and shift
        x = (map_width * (180 + lng) / 360) % map_width + longitude_shift;
    
        // latitude: using the Mercator projection
        lat = lat * Math.PI / 180;  // convert from degrees to radians
        y = Math.log(Math.tan((lat/2) + (Math.PI/4)));  // do the Mercator projection (w/ equator of 2pi units)
        y = (map_height / 2) - (map_width * y / (2 * Math.PI)) + y_pos;   // fit it to our map
    
        x -= x_pos;
        y -= y_pos;
    
        draw_point(x - half_dot, y - half_dot);
    }
    </script>
    </head>
    <body onload="plot_point(40.756, -73.986)">
        <!-- image found at http://www.math.ubc.ca/~israel/m103/mercator.png -->
        <img src="mercator.png" style="position:absolute;top:0px;left:0px">
    </body>
    </html>
    
        2
  •  8
  •   Mike Clark    17 年前

    js中的一个基本转换函数是:

    MAP_WIDTH = 1000;
    MAP_HEIGHT = 446;
    
    function convert(lat, lon){
        var y = ((-1 * lat) + 90) * (MAP_HEIGHT / 180);
        var x = (lon + 180) * (MAP_WIDTH / 360);
        return {x:x,y:y};
    }
    

    这将返回左上角的像素数。

    1. 确保您的图像正确对齐 具有左上角(0,0) 以90*180北对齐* 西
    2. 你的坐标是用N,S,W和E来签名的+
        3
  •  0
  •   hanno    17 年前