leaflet-wms.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. var layerXClickEvent = 0;
  2. var layerYClickEvent = 0;
  3. L.TileLayer.BetterWMS = L.TileLayer.WMS.extend({
  4. onAdd: function (map) {
  5. // Triggered when the layer is added to a map.
  6. // Register a click listener, then do all the upstream WMS things
  7. L.TileLayer.WMS.prototype.onAdd.call(this, map);
  8. map.on('click', this.getFeatureInfo, this);
  9. },
  10. onRemove: function (map) {
  11. // Triggered when the layer is removed from a map.
  12. // Unregister a click listener, then do all the upstream WMS things
  13. L.TileLayer.WMS.prototype.onRemove.call(this, map);
  14. map.off('click', this.getFeatureInfo, this);
  15. },
  16. getFeatureInfo: function (evt) {
  17. layerx = evt.originalEvent.layerX;
  18. layery = evt.originalEvent.layerY;
  19. console.log(evt.originalEvent);
  20. console.log(layerx + " " + layery);
  21. if(layerXClickEvent != layerx || layerYClickEvent != layery) {
  22. layerXClickEvent = layerx;
  23. layerYClickEvent = layery;
  24. console.log("clean");
  25. $("#feature").html("");
  26. }
  27. // Make an AJAX request to the server and hope for the best
  28. var url = this.getFeatureInfoUrl(evt.latlng),
  29. showResults = L.Util.bind(this.showGetFeatureInfo, this);
  30. $.ajax({
  31. url: url,
  32. datatype: 'application/json',
  33. success: function (data, status, xhr) {
  34. var err = typeof data === 'string' ? null : data;
  35. showResults(err, evt.latlng, data);
  36. },
  37. error: function (xhr, status, error) {
  38. showResults(error);
  39. }
  40. });
  41. },
  42. getFeatureInfoUrl: function (latlng) {
  43. var point = this._map.latLngToContainerPoint(latlng, this._map.getZoom()),
  44. size = this._map.getSize(),
  45. params = {
  46. request: 'GetFeatureInfo',
  47. service: 'WMS',
  48. srs: 'EPSG:4326',
  49. styles: this.wmsParams.styles,
  50. transparent: this.wmsParams.transparent,
  51. version: this.wmsParams.version,
  52. format: this.wmsParams.format,
  53. bbox: this._map.getBounds().toBBoxString(),
  54. height: size.y,
  55. width: size.x,
  56. layers: this.wmsParams.layers,
  57. query_layers: this.wmsParams.layers,
  58. feature_count: 50,
  59. info_format: 'application/json'
  60. };
  61. params[params.version === '1.3.0' ? 'i' : 'x'] = point.x;
  62. params[params.version === '1.3.0' ? 'j' : 'y'] = point.y;
  63. return this._url + L.Util.getParamString(params, this._url, true);
  64. //return "{{url('api_feature_json')}}" + L.Util.getParamString(params, this._url, true);
  65. },
  66. showGetFeatureInfo: function (err, latlng, content) {
  67. customShowFeature(this._map, err, latlng, content);
  68. }
  69. });
  70. L.tileLayer.betterWms = function (url, options) {
  71. console.log(url);
  72. return new L.TileLayer.BetterWMS(url, options);
  73. };