Tiled WMS
Tiled WMS示例。 WMS 瓦片 瓦片图层
WMS 既可以用作瓦片(Tile)图层,如本示例所示,也可以用作图像(Image)图层,如 Single Image WMS 示例所示。瓦片可以被缓存,因此浏览器不会重新获取已经查看过的区域的数据。但对于无法感知瓦片的 WMS 服务器,可能会出现注记重复的问题,这种情况下使用单图像 WMS 会产生更好的制图效果。
main.js
js
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import TileLayer from 'ol/layer/WebGLTile.js';
import OSM from 'ol/source/OSM.js';
import TileWMS from 'ol/source/TileWMS.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',
// 国家图层是透明的,因此不要对瓦片做淡入过渡:
transition: 0,
}),
}),
];
const map = new Map({
layers: layers,
target: 'map',
view: new View({
center: [-10997148, 4569099],
zoom: 4,
}),
});index.html
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>瓦片式 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>
<script type="module" src="main.js"></script>
</body>
</html>package.json
json
{
"name": "wms-tiled",
"dependencies": {
"ol": "10.11.0"
},
"devDependencies": {
"vite": "^8.1.5"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}