原文链接及内容
此示例演示了在单击WMS图像层时会触发WMSGetFeatureInfo
请求。此外,layer.getData(pixel)
用于在地图上悬停鼠标至不透明像素时更改鼠标指针样式。
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 import ImageLayer from 'ol/layer/Image.js' ;import ImageWMS from 'ol/source/ImageWMS.js' ;import Map from 'ol/Map.js' ;import View from 'ol/View.js' ;const wmsSource = new ImageWMS ({ url : 'https://ahocevar.com/geoserver/wms' , params : {'LAYERS' : 'ne:ne' }, serverType : 'geoserver' , crossOrigin : 'anonymous' , }); const wmsLayer = new ImageLayer ({ source : wmsSource, }); const view = new View ({ center : [0 , 0 ], zoom : 1 , }); const map = new Map ({ layers : [wmsLayer], target : 'map' , view : view, }); map.on ('singleclick' , function (evt ) { document .getElementById ('info' ).innerHTML = '' ; const viewResolution = (view.getResolution ()); const url = wmsSource.getFeatureInfoUrl ( evt.coordinate , viewResolution, 'EPSG:3857' , {'INFO_FORMAT' : 'text/html' } ); if (url) { fetch (url) .then ((response ) => response.text ()) .then ((html ) => { document .getElementById ('info' ).innerHTML = html; }); } }); map.on ('pointermove' , function (evt ) { if (evt.dragging ) { return ; } const data = wmsLayer.getData (evt.pixel ); const hit = data && data[3 ] > 0 ; map.getTargetElement ().style .cursor = hit ? 'pointer' : '' ; });
界面布局文件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 > WMS GetFeatureInfo (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 >