Openlayers 3 在OpenLayers 3中格式化鼠标位置控件输出
我用以下控件在OpenLayers 3中显示鼠标位置Openlayers 3 在OpenLayers 3中格式化鼠标位置控件输出,openlayers-3,Openlayers 3,我用以下控件在OpenLayers 3中显示鼠标位置 var mousePositionControl = new ol.control.MousePosition({ coordinateFormat: ol.coordinate.createStringXY(2), projection: 'EPSG:4326', undefinedHTML: ' ' }); 但结果显示鼠标位置为Lon,Lat,而不是Lat,Lon 这是一个例子 我怎样才能
var mousePositionControl = new ol.control.MousePosition({
coordinateFormat: ol.coordinate.createStringXY(2),
projection: 'EPSG:4326',
undefinedHTML: ' '
});
但结果显示鼠标位置为Lon,Lat,而不是Lat,Lon
这是一个例子
我怎样才能颠倒顺序,使其为Lat,Lon?您可以将坐标格式“标准函数”更改为自定义函数:
var myFormat = function(dgts)
{
return (
function(coord1) {
var coord2 = [coord1[1], coord1[0]];
return ol.coordinate.toStringXY(coord2,dgts);
});
}
var mousePositionControl = new ol.control.MousePosition({
coordinateFormat: myFormat(2), // <--- change here
projection: 'EPSG:4326',
className: 'custom-mouse-position',
target: document.getElementById('mouse-position'),
undefinedHTML: ' '
});
var myFormat=函数(dgts)
{
返回(
功能(coord1){
var coord2=[coord1[1],coord1[0]];
返回ol.坐标到字符串XY(坐标2,dgts);
});
}
var mousePositionControl=new ol.control.MousePosition({
coordinateFormat:myFormat(2),//另一种格式:
var template = 'LatLon: {y}, {x}';
var mousePositionControl = new ol.control.MousePosition({
coordinateFormat: function(coord) {return ol.coordinate.format(coord, template, 2);},
projection: 'EPSG:4326',
undefinedHTML: ' '
});
对于我来说,添加各种控件(包括Lat、Long)的有效方法是:
var控件=[
新建ol.control.attribute(),
新ol.control.MousePosition({
投影:‘EPSG:4326’,
坐标格式:功能(坐标){
返回ol.coordinate.format(坐标,{y},{x}',4);
}
}),
新的ol.control.ScaleLine(),
新建ol.control.Zoom(),
新的ol.control.ZoomSlider(),
新建ol.control.ZoomToExtent(),
新建ol.control.FullScreen()
];
也有助于以度、分、秒为单位显示:
controls: ol.control.defaults().extend([
new ol.control.ScaleLine({
units: 'nautical'
}),
new ol.control.MousePosition({
coordinateFormat: function(coord) {
return ol.coordinate.toStringHDMS(coord);
},
projection: 'EPSG:4326',
className: 'custom-mouse-position',
target: document.getElementById('mouse-position'),
undefinedHTML: ' '
})
]),
适用于OpenLayers 3.7.0。由于地图视图位于“EGPS:3857”中,因此使用proj4js将坐标重新投影到不同的投影:
var proj1=proj4.defs('EPSG:4326');
var proj2=proj4.defs('EPSG:3857');
var myFormat=函数(数字){
返回(
功能(原始坐标){
var重新投影坐标=项目J4(项目J2,项目J1)。正向(原始坐标);
var switchedCoordinates=[reprojectedCoordinates[1],reprojectedCoordinates[0];
返回ol.坐标到字符串XY(切换坐标,数字);
}
);
}
var mousePositionControl=new ol.control.MousePosition({
协调格式:mojFormat(10),
预测:“ESPG:4326”,
undefinedHTML:“ ”
});
//addControl(mousePositionControl);//相当于setMap
mousePositionControl.setMap(map);