openlayers5-添加经纬度控件

两种不同的经纬度显示方式,通过MousePosition对象,设置coordinateFormat参数

方式一

import {
  createStringXY,
  toStringHDMS
} from 'ol/coordinate.js';

...
  addMousePositionAndScaleControl(map) {
    // let map = this.map;
    var mousePositionControl = new MousePosition({
      // coordinateFormat: createStringXY(4),
      coordinateFormat: function(coord){return toStringHDMS(coord)},
      projection: 'EPSG:4326',
      // comment the following two lines to have the mouse position
      // be placed within the map.
      className: 'custom-mouse-position',
      target: document.getElementById('mouse-position'),
      undefinedHTML:    ' '
    });
    var scaleLineControl = new ScaleLine({
      //设置度量单位为米
      units: 'metric',
      target: 'scalebar',
      className: 'ol-scale-line'
    });
    map.addControl(mousePositionControl);
    //将比例尺控件加入到map中
    map.addControl(scaleLineControl);
  },

效果如下:


image.png

方式二

import {
  createStringXY,
  toStringHDMS
} from 'ol/coordinate.js';

...
  addMousePositionAndScaleControl(map) {
    // let map = this.map;
    var mousePositionControl = new MousePosition({
      coordinateFormat: createStringXY(4),
      projection: 'EPSG:4326',
      // comment the following two lines to have the mouse position
      // be placed within the map.
      className: 'custom-mouse-position',
      target: document.getElementById('mouse-position'),
      undefinedHTML:    ' '
    });
    var scaleLineControl = new ScaleLine({
      //设置度量单位为米
      units: 'metric',
      target: 'scalebar',
      className: 'ol-scale-line'
    });
    map.addControl(mousePositionControl);
    //将比例尺控件加入到map中
    map.addControl(scaleLineControl);
  },
image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容