原文链接及内容
注: 本例使用的功能不是稳定API的一部分,可能会在不同版本之间发生变化。请参考API文档以了解最新版本中支持的内容。

上图中的ecoregions矢量数据是从GeoJSON文件中加载的。
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| import GeoJSON from 'ol/format/GeoJSON.js'; import Layer from 'ol/layer/Layer.js'; import Map from 'ol/Map.js'; import OSM from 'ol/source/OSM.js'; import TileLayer from 'ol/layer/WebGLTile.js'; import VectorSource from 'ol/source/Vector.js'; import View from 'ol/View.js'; import WebGLVectorLayerRenderer from 'ol/renderer/webgl/VectorLayer.js'; import {asArray} from 'ol/color.js'; import {packColor} from 'ol/renderer/webgl/shaders.js';
class WebGLLayer extends Layer { createRenderer() { return new WebGLVectorLayerRenderer(this, { fill: { attributes: { color: function (feature) { const color = asArray(feature.get('COLOR') || '#eee'); color[3] = 0.85; return packColor(color); }, opacity: function () { return 0.6; }, }, }, stroke: { attributes: { color: function (feature) { const color = [...asArray(feature.get('COLOR') || '#eee')]; color.forEach((_, i) => (color[i] = Math.round(color[i] * 0.75))); return packColor(color); }, width: function () { return 1.5; }, opacity: function () { return 1; }, }, }, }); } }
const osm = new TileLayer({ source: new OSM(), });
const vectorLayer = new WebGLLayer({ source: new VectorSource({ url: 'https://openlayers.org/data/vector/ecoregions.json', format: new GeoJSON(), }), });
const map = new Map({ layers: [osm, vectorLayer], target: 'map', view: new View({ center: [0, 0], zoom: 1, }), });
|
界面布局文件index.html
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL Vector Layer</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 src="https://cdn.jsdelivr.net/npm/elm-pep@1.0.6/dist/elm-pep.js"></script> <script type="module" src="main.js"></script> </body> </html>
|