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
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容