【问题标题】:How to convert Openlayers 6.5 Mouse position control from LonLat to MGRS?如何将 Openlayers 6.5 鼠标位置控制从 LonLat 转换为 MGRS?
【发布时间】:2021-09-11 23:30:12
【问题描述】:

我正在使用这个控件

var mousePositionControl = new ol.control.MousePosition({
    coordinateFormat: ol.coordinate.createStringXY(2),
    projection: 'EPSG:4326',   
    undefinedHTML: ' '
});

【问题讨论】:

    标签: maps coordinates mouseevent openlayers-6


    【解决方案1】:

    如果您使用 mgrs 库 https://github.com/proj4js/mgrs,它采用 [lon, lat] 坐标数组并返回 MGRS 字符串,您可以将其 forward 方法用作 coordinateFormat

    <!doctype html>
    <html lang="en">
      <head>
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.5.0/css/ol.css" type="text/css">
        <style>
          html, body, .map {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
          }
        </style>
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.5.0/build/ol.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/mgrs@1.0.0/dist/mgrs.min.js"></script>
      </head>
      <body>
        <div id="map" class="map"></div>
        <script type="text/javascript">
          var mousePositionControl = new ol.control.MousePosition({
            coordinateFormat: mgrs.forward,
            projection: 'EPSG:4326',
            undefinedHTML: '&nbsp;'
          });
          var map = new ol.Map({
            target: 'map',
            controls: ol.control.defaults().extend([mousePositionControl]),
            layers: [
              new ol.layer.Tile({
                source: new ol.source.OSM()
              })
            ],
            view: new ol.View({
              center: [0, 0],
              zoom: 2
            })
          });
        </script>
      </body>
    </html>

    【讨论】:

    • 这工作非常感谢你,我想知道如何将鼠标位置div的内容复制到剪贴板?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多