【问题标题】:Answer to converting lat/long to x/y on mercator not working for me when changing location from Germany to US将位置从德国更改为美国时,在墨卡托上将 lat/long 转换为 x/y 的答案对我不起作用
【发布时间】:2014-03-17 11:21:58
【问题描述】:

我正在使用 Raphael 来自这篇文章 (https://stackoverflow.com/a/10401734/3321095) 的回答将纬度/经度转换为墨卡托地图上绘制的 xy 坐标。 Raphael 的示例使用了德国汉堡的一个地区。我测试了它,它确实有效。然后我将其更改为在美国境内找到一个点,但坐标始终超出图像的大小。有人可以帮忙吗?

<script type="text/javascript">
        var mapWidth = 749; //1500;
        var mapHeight = 462; //1577;

        var mapLonLeft = 125; //9.8;
        var mapLonRight = 65 //10.2;
        var mapLonDelta = mapLonRight - mapLonLeft;

        var mapLatBottom = 25 //53.45;
        var mapLatBottomDegree = mapLatBottom * Math.PI / 180;

        function convertGeoToPixel(lat, lon)
        {
            var position = new Array(2);

            var x = (lon - mapLonLeft) * (mapWidth / mapLonDelta);

            var lat = lat * Math.PI / 180;
            var worldMapWidth = ((mapWidth / mapLonDelta) * 360) / (2 * Math.PI);
            var mapOffsetY = (worldMapWidth / 2 * Math.log((1 + Math.sin(mapLatBottomDegree)) / (1 - Math.sin(mapLatBottomDegree))));

            var y = mapHeight - ((worldMapWidth / 2 * Math.log((1 + Math.sin(lat)) / (1 - Math.sin(lat)))) - mapOffsetY);

            position[0] = x;
            position[1] = y;

            return position;
        }

        var coordinates = convertGeoToPixel(30.274333164300643, -97.74064064025879); //convertGeoToPixel(53.7, 9.95);
        alert("x: " + coordinates[0] + " y: " + coordinates[1]);
    </script>

【问题讨论】:

  • 我不妨问问你得到了什么输出,你期望什么输出。看起来那个地方就是德克萨斯州奥斯汀的国会大厦。这是应该的吗?
  • 是的,应该是。我使用的地图是 749x462,但我从上面的代码中得到的点是 x:2781 和 y:536。它应该更接近 x:373 和 y:324。

标签: latitude-longitude


【解决方案1】:

希望你在去年就明白了这一点。你的代码帮助我完成了一个类似的项目。您的代码缺少减号,应如下所示:

    var mapLonLeft = -125; //9.8;
    var mapLonRight = -65 //10.2;

在美国,经度为负数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 2015-05-06
    相关资源
    最近更新 更多