原文链接及内容

此示例使用ol/layer/VectorImage
在交互和动画过程中实现更快的渲染,但代价是渲染精度较低。
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| import GeoJSON from 'ol/format/GeoJSON.js'; import Map from 'ol/Map.js'; import VectorImageLayer from 'ol/layer/VectorImage.js'; import VectorLayer from 'ol/layer/Vector.js'; import VectorSource from 'ol/source/Vector.js'; import View from 'ol/View.js'; import {Fill, Stroke, Style} from 'ol/style.js';
const style = new Style({ fill: new Fill({ color: '#eeeeee', }), });
const vectorLayer = new VectorImageLayer({ background: '#1a2b39', imageRatio: 2, source: new VectorSource({ url: 'https://openlayers.org/data/vector/ecoregions.json', format: new GeoJSON(), }), style: function (feature) { const color = feature.get('COLOR') || '#eeeeee'; style.getFill().setColor(color); return style; }, });
const map = new Map({ layers: [vectorLayer], target: 'map', view: new View({ center: [0, 0], zoom: 1, }), });
const featureOverlay = new VectorLayer({ source: new VectorSource(), map: map, style: new Style({ stroke: new Stroke({ color: 'rgba(255, 255, 255, 0.7)', width: 2, }), }), });
let highlight; const displayFeatureInfo = function (pixel) { const feature = map.forEachFeatureAtPixel(pixel, function (feature) { return feature; });
const info = document.getElementById('info'); if (feature) { info.innerHTML = feature.get('ECO_NAME') || ' '; } else { info.innerHTML = ' '; }
if (feature !== highlight) { if (highlight) { featureOverlay.getSource().removeFeature(highlight); } if (feature) { featureOverlay.getSource().addFeature(feature); } highlight = feature; } };
map.on('pointermove', function (evt) { if (evt.dragging) { return; } const pixel = map.getEventPixel(evt.originalEvent); displayFeatureInfo(pixel); });
map.on('click', function (evt) { displayFeatureInfo(evt.pixel); });
|
界面布局文件index.html
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Vector Image 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> <div id="info"> </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>
|