原文链接及内容

运行界面

影像数据可以使用服务的特定加载器(loader)来配置,默认地,加载器将影像作为解码的ImageBitmap返回。当改用ol/source/Image.load()函数时,SVG图像将在放大/缩小时被缩放,这是因为它们将被直接用作HTMLImageElement而不是ImageBitmap

main.js代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import ImageSource from 'ol/source/Image.js';
import Map from 'ol/Map.js';
import OSM from 'ol/source/OSM.js';
import View from 'ol/View.js';
import {Image as ImageLayer, Tile as TileLayer} from 'ol/layer.js';
import {createLoader} from 'ol/source/wms.js';
import {load} from 'ol/Image.js';

const layers = [
new TileLayer({
source: new OSM(),
}),
new ImageLayer({
extent: [-13884991, 2870341, -7455066, 6338219],
source: new ImageSource({
loader: createLoader({
url: 'https://ahocevar.com/geoserver/wms',
params: {'LAYERS': 'topp:states', 'FORMAT': 'image/svg+xml'},
ratio: 1,
load: load,
}),
}),
}),
];
const map = new Map({
layers: layers,
target: 'map',
view: new View({
center: [-10997148, 4569099],
zoom: 4,
}),
});

界面布局文件index.html代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>WMS loader with SVG format</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>