leaflet-wms.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. // Make an AJAX request to the server and hope for the best
  18. var url = this.getFeatureInfoUrl(evt.latlng),
  19. showResults = L.Util.bind(this.showGetFeatureInfo, this);
  20. $.ajax({
  21. url: url,
  22. datatype: 'application/json',
  23. success: function (data, status, xhr) {
  24. var err = typeof data === 'string' ? null : data;
  25. showResults(err, evt.latlng, data);
  26. },
  27. error: function (xhr, status, error) {
  28. showResults(error);
  29. }
  30. });
  31. },
  32. getFeatureInfoUrl: function (latlng) {
  33. var point = this._map.latLngToContainerPoint(latlng, this._map.getZoom()),
  34. size = this._map.getSize(),
  35. params = {
  36. request: 'GetFeatureInfo',
  37. service: 'WMS',
  38. srs: 'EPSG:4326',
  39. styles: this.wmsParams.styles,
  40. transparent: this.wmsParams.transparent,
  41. version: this.wmsParams.version,
  42. format: this.wmsParams.format,
  43. bbox: this._map.getBounds().toBBoxString(),
  44. height: size.y,
  45. width: size.x,
  46. layers: this.wmsParams.layers,
  47. query_layers: this.wmsParams.layers,
  48. feature_count: 1,
  49. info_format: 'application/json'
  50. };
  51. params[params.version === '1.3.0' ? 'i' : 'x'] = point.x;
  52. params[params.version === '1.3.0' ? 'j' : 'y'] = point.y;
  53. return this._url + L.Util.getParamString(params, this._url, true);
  54. //return "{{url('api_feature_json')}}" + L.Util.getParamString(params, this._url, true);
  55. },
  56. showGetFeatureInfo: function (err, latlng, content) {
  57. //customShowFeature(this._map, err, latlng, content);
  58. }
  59. });
  60. L.tileLayer.betterWms = function (url, options) {
  61. return new L.TileLayer.BetterWMS(url, options);
  62. };