gpt4 book ai didi

javascript - 如何通过复选框在打开的图层 map 上添加图层?

转载 作者:行者123 更新时间:2023-12-03 02:01:50 24 4
gpt4 key购买 nike

<html>
<head>
<title>Single Image WMS</title>
<link rel="stylesheet" href="https://openlayers.org/en/v4.6.4/css/ol.css" type="text/css">
<!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
<script src="https://openlayers.org/en/v4.6.4/build/ol.js"></script>
</head>
<body>
<div id="map" class="map"></div>
<script>
var layers = [
new ol.layer.Tile({
source: new ol.source.OSM()
new ol.layer.Image({
//extent: [-13884991, 2870341, -7455066, 6338219],
source: new ol.source.ImageWMS({
url: '//',
params: {'LAYERS': '//'},
ratio: 1,
serverType: 'geoserver'
})
}),
new ol.layer.Image({
//extent: [-13884991, 2870341, -7455066, 6338219],
source: new ol.source.ImageWMS({
url: '//',
params: {'LAYERS': '//'},
ratio: 1,
serverType: 'geoserver'
})
}),
new ol.layer.Image({
//extent: [-13884991, 2870341, -7455066, 6338219],
source: new ol.source.ImageWMS({
url: '//',
params: {'LAYERS': '//'},
ratio: 1,
serverType: 'geoserver'
})
}),
new ol.layer.Image({
//extent: [-13884991, 2870341, -7455066, 6338219],
source: new ol.source.ImageWMS({
url: '//',
params: {'LAYERS': '//'},
ratio: 1,
serverType: 'geoserver'
})
}),
new ol.layer.Image({
//extent: [-13884991, 2870341, -7455066, 6338219],
source: new ol.source.ImageWMS({
url: '//',
params: {'LAYERS': '//'},
ratio: 1,
serverType: 'geoserver'
})
}),

new ol.layer.Image({
//extent: [-13884991, 2870341, -7455066, 6338219],
source: new ol.source.ImageWMS({
url: '//s',
params: {'LAYERS': '//'},
ratio: 1,
serverType: 'geoserver'
})
}),

new ol.layer.Image({
//extent: [-13884991, 2870341, -7455066, 6338219],
source: new ol.source.ImageWMS({
url: '//',
params: {'LAYERS': '//'},
ratio: 1,
serverType: 'geoserver'
})
})
];

var map = new ol.Map({
layers: layers,
target: 'map',
view: new ol.View({
center: ol.proj.fromLonLat([73.6608, 29.8820]),
zoom: 9
})
});

</script>
</body>
</html>

所有图层都必须采用复选框的形式,并且在选中该复选框后,必须将以下内容添加到 map 中?所有图层都必须采用复选框的形式,并且在选中该框后,必须将以下内容添加到 map 中?所有图层都必须采用复选框的形式,并且在选中该复选框后,必须将以下内容添加到 map 中?

最佳答案

您首先需要(Layer Switcher Control)js+css,可以从这个链接下载:

https://github.com/GitiFarazPishro/ol3-ext/tree/gh-pages/control

然后您可以在控制切换器中定义图层,如下所示:

//for the baselayer
var Base = {}

Base['1'] = new ol.layer.Tile({
source: new ol.source.OSM(),
baseLayer: true,
displayInLayerSwitcher: true,
visible: true,
title: "Base Layer"
});

//for the other layers:

var questionLayers = {};

questionLayers['1'] = new ol.layer.Tile({
source: new ol.source.OSM(),
visible: false,
title: "1"
});

questionLayers['2'] = new ol.layer.Image({
//extent: [-13884991, 2870341, -7455066, 6338219],
source: new ol.source.ImageWMS({
url: http://someurl
params: {},
ratio: 1,
serverType: 'geoserver',
}),
visible: false,
title: "2"
});

questionLayers['3'] = new ol.layer.Image({
//extent: [-13884991, 2870341, -7455066, 6338219],
source: new ol.source.ImageWMS({
url: http://someurl
params: {},
ratio: 1,
serverType: 'geoserver',
}),
visible: false,
title: "3"
});

questionLayers['4'] = new ol.layer.Image({
//extent: [-13884991, 2870341, -7455066, 6338219],
source: new ol.source.ImageWMS({
url: http://someurl
params: {},
ratio: 1,
serverType: 'geoserver',
}),
visible: false,
title: "4"
});

questionLayers['5'] = new ol.layer.Image({
//extent: [-13884991, 2870341, -7455066, 6338219],
source: new ol.source.ImageWMS({
url: http://someurl
params: {},
ratio: 1,
serverType: 'geoserver',
}),
visible: false,
title: "5"
});

questionLayers['6'] = new ol.layer.Image({
//extent: [-13884991, 2870341, -7455066, 6338219],
source: new ol.source.ImageWMS({
url: http://someurl
params: {},
ratio: 1,
serverType: 'geoserver',
}),
visible: false,
title: "6"
});
questionLayers['7'] = new ol.layer.Image({
//extent: [-13884991, 2870341, -7455066, 6338219],
source: new ol.source.ImageWMS({
url: http://someurl
params: {},
ratio: 1,
serverType: 'geoserver',
}),
visible: false,
title: "7"
});

questionLayers['8'] = new ol.layer.Image({
//extent: [-13884991, 2870341, -7455066, 6338219],
source: new ol.source.ImageWMS({
url: http://someurl
params: {},
ratio: 1,
serverType: 'geoserver',
}),
visible: false,
title: "8"
});

var stack = new ol.layer.Group({
title: 'Question Layers',
openInLayerSwitcher: false,
visible: false,
layers: Object.values(questionLayers)});

定义您的 map 和控件:

// this function to take the layers as array and add them into the switcher
function LayersToArray(layer) {
var array = [];
for( key in layer)
array[array.length]= layer[key];
return array;
}

var map = new ol.Map({
target: 'map',
logo:false,
view: new ol.View({
center: ol.proj.fromLonLat([73.6608, 29.8820]),
zoom: 9
}),

controls: ol.control.defaults().extend([
new ol.control.LayerSwitcher,
]),
layers: LayersToArray(Base).concat( [stack] )

});

你的 map 应该是这样的:

enter image description here

关于javascript - 如何通过复选框在打开的图层 map 上添加图层?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49976629/

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