本示例演示加载已缓存的 WMS 图层.
WMS 可以作为瓦片图层使用,也可以作为图像图层使用,比如这个示例中 Single Image WMS example。 瓦片可以缓存使用,因此浏览器将不会重新请求那些已经看过的瓦片数据。 但是对于 WMS 服务来说,不会关注瓦片可能出现重复的问题,因此,这种情况下,使用单个图像 WMS 效果会更好。
import Map from 'ol/Map.js';
import OSM from 'ol/source/OSM.js';
import TileLayer from 'ol/layer/Tile.js';
import TileWMS from 'ol/source/TileWMS.js';
import View from 'ol/View.js';
const layers = [
new TileLayer({
source: new OSM(),
}),
new TileLayer({
extent: [-13884991, 2870341, -7455066, 6338219],
source: new TileWMS({
url: 'https://ahocevar.com/geoserver/wms',
params: {'LAYERS': 'topp:states', 'TILED': true},
serverType: 'geoserver',
// Countries have transparency, so do not fade tiles:
transition: 0,
}),
}),
];
const map = new Map({
layers: layers,
target: 'map',
view: new View({
center: [-10997148, 4569099],
zoom: 4,
}),
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tiled WMS</title>
<link rel="stylesheet" href="node_modules/ol/ol.css">
<style>
.map {
width: 100%;
height: 400px;
}
</style>
</head>
<body>
<div id="map" class="map"></div>
<!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
<script src="./resources/elm-pep.js"></script>
<script type="module" src="main.js"></script>
</body>
</html>
{
"name": "wms-tiled",
"dependencies": {
"ol": "7.3.0"
},
"devDependencies": {
"vite": "^3.2.3"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}