leaflet_wms.html.twig 2.6 KB

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