1. 程式人生 > >WMS GetFeatureInfo (Tile Layer)——WMS獲取要素信息(瓦片圖層)

WMS GetFeatureInfo (Tile Layer)——WMS獲取要素信息(瓦片圖層)

判斷 each event doctype sin cross comment css ram

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>WMS GetFeatureInfo (Tile Layer)</title>
  5. <link rel="stylesheet" href="https://openlayers.org/en/v4.2.0/css/ol.css" type="text/css">
  6. <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
  7. <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
  8. <script src="https://openlayers.org/en/v4.2.0/build/ol.js"></script>
  9. </head>
  10. <body>
  11. <div id="map" class="map"></div>
  12. <div id="info"> </div>
  13. <script>
  14. // WMS瓦片數據源
  15. var wmsSource = new ol.source.TileWMS({
  16. url: ‘https://ahocevar.com/geoserver/wms‘,
  17. params: {‘LAYERS‘: ‘ne:ne‘, ‘TILED‘: true},
  18. serverType: ‘geoserver‘,
  19. crossOrigin: ‘anonymous‘
  20. });
  21. // WMS瓦片圖層
  22. var wmsLayer = new ol.layer.Tile({
  23. source: wmsSource
  24. });
  25. var view = new ol.View({
  26. center: [0, 0],
  27. zoom: 1
  28. });
  29. var map = new ol.Map({
  30. layers: [wmsLayer],
  31. target: ‘map‘,
  32. view: view
  33. });
  34. // 在地圖上單擊的時候獲取要素信息
  35. map.on(‘singleclick‘, function(evt) {
  36. document.getElementById(‘info‘).innerHTML = ‘‘;
  37. var viewResolution = /** @type {number} */ (view.getResolution());
  38. var url = wmsSource.getGetFeatureInfoUrl(
  39. evt.coordinate, viewResolution, ‘EPSG:3857‘,
  40. {‘INFO_FORMAT‘: ‘text/html‘});
  41. if (url) {
  42. document.getElementById(‘info‘).innerHTML =
  43. ‘<iframe seamless src="‘ + url + ‘"></iframe>‘;
  44. }
  45. });
  46. // 根據map的forEachLayerAtPixel方法來判斷當前像素位置是否有要素,如果有則改變鼠標樣式
  47. map.on(‘pointermove‘, function(evt) {
  48. if (evt.dragging) {
  49. return;
  50. }
  51. var pixel = map.getEventPixel(evt.originalEvent);
  52. var hit = map.forEachLayerAtPixel(pixel, function() {
  53. return true;
  54. });
  55. map.getTargetElement().style.cursor = hit ? ‘pointer‘ : ‘‘;
  56. });
  57. </script>
  58. </body>
  59. </html>

WMS GetFeatureInfo (Tile Layer)——WMS獲取要素信息(瓦片圖層)