【问题标题】:Leaflet custom coordinates on image图片上的传单自定义坐标
【发布时间】:2016-04-10 21:12:12
【问题描述】:

我有一个尺寸为 8576x8576px 的图像,我想让坐标匹配 1:1。我还想要图像中心的坐标 0,0(现在中心是 -128,128)。我也想显示坐标。我想为用户插入坐标放置一个定位按钮,然后在地图上找到它们。 像这样的东西:http://xero-hurtworld.com/map_steam.php (我使用相同的图像但更大)。我设置的 tile 大小为 268px。

到目前为止我的代码:

https://jsfiddle.net/ze62dte0/

<!DOCTYPE html>
<html>
  <head>
    <title>Map</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
    <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.css" />
    <!--[if lte IE 8]>
    <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.ie.css" />
    <![endif]-->
    <script src="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.js" charset="utf-8"></script>
    <script>
      function init() {
        var mapMinZoom = 0;
        var mapMaxZoom = 3;
        var map = L.map('map', {
          maxZoom: mapMaxZoom,
          minZoom: mapMinZoom,
          crs: L.CRS.Simple
        }).setView([0, 0], mapMaxZoom);



    window.latLngToPixels = function(latlng){
    return window.map.project([latlng.lat,latlng.lng], window.map.getMaxZoom());
    };
    window.pixelsToLatLng = function(x,y){
    return window.map.unproject([x,y], window.map.getMaxZoom());
    };

        var mapBounds = new L.LatLngBounds(
            map.unproject([0, 8576], mapMaxZoom),
            map.unproject([8576, 0], mapMaxZoom));

        map.fitBounds(mapBounds);
        L.tileLayer('{z}/{x}/{y}.jpg', {
          minZoom: mapMinZoom, maxZoom: mapMaxZoom,
          bounds: mapBounds,
          noWrap: true,
          tms: false
        }).addTo(map);

        L.marker([0, 0]).addTo(map).bindPopup("Zero");

        L.marker([-128, 128]).addTo(map).bindPopup("center");

        var popup = L.popup();

        <!-- Click pop-up>
        var popup = L.popup();

        function onMapClick(e) {
            popup
            .setLatLng(e.latlng)
            .setContent("You clicked in " + e.latlng.toString ())
            .openOn(map);
        }

        map.on('click', onMapClick);

      }
    </script>
    <style>
      html, body, #map { width:100%; height:100%; margin:0; padding:0; }
    </style>
  </head>
  <body onload="init()">
    <div id="map"></div>
  </body>
</html>

【问题讨论】:

    标签: javascript dictionary leaflet


    【解决方案1】:

    如果我理解正确,您需要一个类似于L.CRS.Simple 的 CRS,它放置图块 0/0/0(图块大小 268 像素,即 8576 / 2⁵),以便:

    • 位置[0, 0] 位于该图块的中心。
    • 整个世界(即整个图块 0/0/0)从位置 [-8576/2, -8576/2][8576/2, 8576/2]

    您只需要使用适当的转换来调整L.CRS.Simple,以说明 1/2⁵ = 1/32(而不仅仅是 1)的比例和 8576 * 1/32 / 2 = 268 / 的偏移量2 = 134(而不是 0.5)。

    L.CRS.MySimple = L.extend({}, L.CRS.Simple, {
      transformation: new L.Transformation(1 / 32, 134, -1 / 32, 134)
    });
    
    var map = L.map('map', {
      maxZoom: mapMaxZoom,
      minZoom: mapMinZoom,
      crs: L.CRS.MySimple
    }).setView([0, 0], mapMaxZoom);
    

    演示:http://plnkr.co/edit/5SQqp7SP4nf8muPM5iso?p=preview(我使用 Plunker 而不是 jsfiddle,因为您提供了带有 HTML 的完整页面代码,而 jsfiddle 希望您将 HTML、CSS 和 JavaScript 代码分成单独的块)。

    至于显示坐标和“定位”按钮,它很容易实现,因此与您提到的示例相似。如果您需要帮助,请随时提出新问题。

    在上面的演示中,我使用Leaflet.Coordinates plugin 来快速实现这两个功能(参见地图左下角的控件;您必须开始在地图上移动鼠标才能显示坐标;单击该控件打开编辑模式)。


    编辑:

    对于 Leaflet.Coordinates 插件,它将显示的坐标经度包裹在 [-180; 180] 度。

    在坐标不是度数的情况下,包裹经度没有意义。

    我认为这是造成点击弹窗与控件坐标不一致的原因。

    只需修补插件代码以防止包装:

    // Patch first to avoid longitude wrapping.
    L.Control.Coordinates.include({
      _update: function(evt) {
        var pos = evt.latlng,
          opts = this.options;
        if (pos) {
          //pos = pos.wrap(); // Remove that instruction.
          this._currentPos = pos;
          this._inputY.value = L.NumberFormatter.round(pos.lat, opts.decimals, opts.decimalSeperator);
          this._inputX.value = L.NumberFormatter.round(pos.lng, opts.decimals, opts.decimalSeperator);
          this._label.innerHTML = this._createCoordinateLabel(pos);
        }
      }
    });
    

    更新的演示:http://plnkr.co/edit/M3Ru0xqn6AxAaSb4kIJU?p=preview

    【讨论】:

    • 非常感谢!有效!正是我想要的方式!
    • 不客气,感谢您的反馈。请您也考虑支持并接受这个答案吗?这是感谢人们的 Stack Overflow 系统,尽管当然也非常感谢您的好评! :-)
    • 我唯一注意到的是左下方控件显示的坐标与单击地图时显示的坐标有所不同。例如,如果我在纽约 (1208, -1864) 上放置一个标记,则控件上显示的坐标是不同的。我怎样才能让它们看起来一样?再次感谢!
    • Leaflet.Coordinates 插件内的经度上有一个wrap。我忘了为你的情况修补它,让我编辑演示给你看。
    • 有趣,另一个小问题,坐标不应该等于笛卡尔平面吗?或者像我上面提到的例子,在这里:xero-hurtworld.com/map_steam.php
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多