OpenLayer 3 鼠标位置坐标显示控件

时间:2024-01-10 14:01:38
<body>
<div id="map">
<div id="mouse-position"></div>
</div>
<script>
var map=new ol.Map({
target:"map",
layer:[],
view:new ol.View({
center:[0,0],
zoom:2
})
//control:ol.control.defaults().extend([mousePositionControl])
})
var tileLayer=new ol.layer.Tile({
source:new ol.source.OSM()
})
map.addLayer(tileLayer);
var mousePositionControl=new ol.control.MousePosition({
coordinateFormat:ol.coordinate.createStringXY(4),
projection:"EPSG:4326",
className:"custom-mouse-position",
target:document.getElementById("mouse-position"),
undefinedHTML:"&nbsp"
});
map.addControl(mousePositionControl);
</script>
</body>

OpenLayer 3 鼠标位置坐标显示控件

疑问:不知道为什么直接通过设置controls参数加载鼠标位置控件就有错,但是调用map.addControl方法加载就对了;layer也是这样