Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. //declare the layer
  2. var osmLayer = new ol.layer.Tile({
  3. source: new ol.source.OSM()
  4. });
  5. //asign the listeners on the source of tile layer
  6. osmLayer.getSource().on('tileloadstart', function(event) {
  7. //replace with your custom action
  8. document.getElementById('tilesloaderindicatorimg').src = 'css/images/tree_loading.gif';
  9. });
  10.  
  11. osmLayer.getSource().on('tileloadend', function(event) {
  12. //replace with your custom action
  13. document.getElementById('tilesloaderindicatorimg').src = 'css/images/ok.png';
  14. });
  15. osmLayer.getSource().on('tileloaderror', function(event) {
  16. //replace with your custom action
  17. document.getElementById('tilesloaderindicatorimg').src = 'css/images/no.png';
  18. });
  19.  
  20. //declare the layer
  21. var wmsLayer = new ol.layer.Image({
  22. source: new ol.source.ImageWMS({
  23. attributions: [new ol.Attribution({
  24. html: '© ' +
  25. '<a href="http://www.geo.admin.ch/internet/geoportal/' +
  26. 'en/home.html">' +
  27. 'National parks / geo.admin.ch</a>'
  28. })],
  29. crossOrigin: 'anonymous',
  30. params: {'LAYERS': 'ch.bafu.schutzgebiete-paerke_nationaler_bedeutung'},
  31. serverType: 'mapserver',
  32. url: 'http://wms.geo.admin.ch/'
  33. })
  34. });
  35.  
  36. //and now asign the listeners on the source of it
  37. var lyrSource = wmsLayer.getSource();
  38. lyrSource.on('imageloadstart', function(event) {
  39. console.log('imageloadstart event',event);
  40. //replace with your custom action
  41. var elemId = event.target.params_.ELEMENTID;
  42. document.getElementById(elemId).src = 'css/images/tree_loading.gif';
  43. });
  44.  
  45. lyrSource.on('imageloadend', function(event) {
  46. console.log('imageloadend event',event);
  47. //replace with your custom action
  48. var elemId = event.target.params_.ELEMENTID;
  49. document.getElementById(elemId).src = 'css/images/ok.png';
  50. });
  51.  
  52. lyrSource.on('imageloaderror', function(event) {
  53. console.log('imageloaderror event',event);
  54. //replace with your custom action
  55. var elemId = event.target.params_.ELEMENTID;
  56. document.getElementById(elemId).src = 'css/images/no.png';
  57. });
  58.  
  59. //declare the vector source
  60. sourceVector = new ol.source.Vector({
  61. loader: function (extent) {
  62. //START LOADING
  63. //place here any actions on start loading layer
  64. document.getElementById('laodingcont').innerHTML = "<font color='orange'>start loading.....</font>";
  65. $.ajax('http://demo.opengeo.org/geoserver/wfs', {
  66. type: 'GET',
  67. data: {
  68. service: 'WFS',
  69. version: '1.1.0',
  70. request: 'GetFeature',
  71. typename: 'water_areas',
  72. srsname: 'EPSG:3857',
  73. bbox: extent.join(',') + ',EPSG:3857'
  74. }
  75. }).done(loadFeatures)
  76. .fail(function () {
  77. //FAIL LOADING
  78. //place here any actions on fail loading layer
  79. document.getElementById('laodingcont').innerHTML = "<font color='red'>error loading vector layer.</font>";
  80. });
  81. },
  82. strategy: ol.loadingstrategy.bbox
  83. });
  84.  
  85. //once we have a success responce, we need to parse it and add fetaures on map
  86. function loadFeatures(response) {
  87. formatWFS = new ol.format.WFS(),
  88. sourceVector.addFeatures(formatWFS.readFeatures(response));
  89. //FINISH LOADING
  90. document.getElementById('laodingcont').innerHTML = "<font color='green'>finish loading vector layer.</font>";
  91. }
  92.  
  93. function spin(active) {
  94. if (active) {
  95. // start spinning the spinner
  96. } else {
  97. // stop spinning the spinner
  98. }
  99. }
  100.  
  101. // Toggle spinner on layer loading
  102. layer.on('change', function() {
  103. spin();
  104. });
  105. layer.getSource().on('change', function() {
  106. spin(false);
  107. });
  108.  
  109. if (source instanceof ol.source.Vector) {
  110. source.on("change", function () {
  111. //console.log("Vector change, state: " + source.getState());
  112. switch (source.getState()) {
  113. case "loading":
  114. $("#ajaxSpinnerImage").show();
  115. break;
  116. default:
  117. $("#ajaxSpinnerImage").hide();
  118. }
  119. });
  120. }
  121.  
  122. if (layer instanceof ol.layer.Vector) {
  123. layer.on("precompose", function () {
  124. $("#ajaxSpinnerImage").show();
  125. });
  126. layer.on("render", function () {
  127. $("#ajaxSpinnerImage").hide();
  128. });
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement