gpt4 book ai didi

openlayers bindTo 事件不起作用

转载 作者:行者123 更新时间:2023-12-01 10:39:20 24 4
gpt4 key购买 nike

我有一个问题,切换复选框不起作用,控制台显示如下。 代码:

<!doctype html>
<head>
<title> Me OpenStreetMap </title>
<link rel="stylesheet" href="../assets/ol3/css/ol.css" type="text/css" />
<link rel="stylesheet" href="../assets/ol3/css/samples.css" type="text/css" />
</head>
<body>

<div id="map"></div>
<input type="checkbox" id="visible" checked />Toggle layer Visibility

<script src="../assets/ol3/js/ol.js"></script>
<script>
var center = new ol.proj.transform([11.57,3.86], 'EPSG:4326','EPSG:3857');
var view = new ol.View({
zoom: 6,
center: center
});
var layer = new ol.layer.Tile({
source: new ol.source.OSM()
});

var map = new ol.Map({
target: 'map',
layers: [layer],
view: view
});

// bind a checkbox with id 'visible' to a layer's visibility
var visible = new ol.dom.Input(document.getElementById('visible')); /* line 30 */
visible.bindTo('checked', layer, 'visible');

</script>

</body>
</html>

map 出现了,但是复选框没有反应,控制台日志说:

Uncaught TypeError: Cannot read property 'Input' of undefined
(anonymous function) @ events.html:30

我经历了这个,但我还是不明白 OpenLayers - Uncaught TypeError: Cannot read property 'div' of undefined

最佳答案

如果要使用此功能,请确保您的库版本低于 3.5.0,因为 ol.dom.Input 已在 3.5.0 版本中删除:https://github.com/openlayers/ol3/releases/tag/v3.5.0

The experimental ol.dom.Input component has been removed. If you need to synchronize the state of a dom Input element with an ol.Object, this can be accomplished using listeners for change events. For example, you might bind the state of a checkbox type input with a layer's visibility like this:

var layer = new ol.layer.Tile();
var checkbox = document.querySelector('#checkbox');

checkbox.addEventListener('change', function() {
var checked = this.checked;
if (checked !== layer.getVisible()) {
layer.setVisible(checked);
}
});

layer.on('change:visible', function() {
var visible = this.getVisible();
if (visible !== checkbox.checked) {
checkbox.checked = visible;
}
});

关于openlayers bindTo 事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31501924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com