方式一
import { format } from 'ol/coordinate';
import MousePosition from "ol/control/MousePosition.js";
//鼠标获取坐标控件
const mousePositionControl = new MousePosition({
coordinateFormat: function (coordinate) {
return format(coordinate, '经度:{x} 纬度:{y}', 2);
},
projection: 'EPSG:4326',
className: 'custom-mouse-position',
target: document.getElementById('mouse-position'),
undefinedHTML: ' '
});
//添加控件到地图
map.addControl(mousePositionControl);
格式像这样:经度:165.23 纬度:13.71
方式二
import { createStringXY } from 'ol/coordinate.js';
import MousePosition from "ol/control/MousePosition.js";
//鼠标获取坐标
const mousePositionControl = new MousePosition({
coordinateFormat: createStringXY(2),
projection: 'EPSG:4326',
className: 'custom-mouse-position',
target: document.getElementById('mouse-position'),
undefinedHTML: ' '
});
map.addControl(mousePositionControl);
格式像这样:165.23 13.71