api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

geojson-layer.js 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* global L */
  2. (function() {
  3. L.GeoJSONLayer = L.GeoJSON.extend({
  4. includes: L.Evented.prototype,
  5. map: null,
  6. options: {
  7. },
  8. initialize(extraOptions, options) {
  9. L.GeoJSON.prototype.initialize.call(this, [], options);
  10. L.Util.setOptions(this, extraOptions);
  11. },
  12. _reload: function() {
  13. if (this.map) {
  14. var url = this._expand(this.options.url);
  15. this._ajax('GET', url, false, this._update.bind(this));
  16. }
  17. },
  18. _update: function(geoData) {
  19. this.clearLayers();
  20. this.addData(geoData);
  21. },
  22. onAdd: function(map) {
  23. L.GeoJSON.prototype.onAdd.call(this, map);
  24. this.map = map;
  25. map.on('moveend zoomend refresh', this._reload, this);
  26. this._reload();
  27. },
  28. onRemove: function(map) {
  29. map.off('moveend zoomend refresh', this._reload, this);
  30. this.map = null;
  31. L.GeoJSON.prototype.onRemove.call(this, map);
  32. },
  33. _expand: function(template) {
  34. var bbox = this._map.getBounds();
  35. var southWest = bbox.getSouthWest();
  36. var northEast = bbox.getNorthEast();
  37. var bboxStr = bbox.toBBoxString();
  38. var coords = {
  39. lat1: southWest.lat,
  40. lon1: southWest.lng,
  41. lat2: northEast.lat,
  42. lon2: northEast.lng,
  43. bbox: bboxStr
  44. };
  45. return L.Util.template(template, coords);
  46. },
  47. _ajax: function(method, url, data, callback) {
  48. var request = new XMLHttpRequest();
  49. request.open(method, url, true);
  50. request.onreadystatechange = function() {
  51. if (request.readyState === 4 && request.status === 200) {
  52. callback(JSON.parse(request.responseText));
  53. }
  54. };
  55. if (data) {
  56. request.setRequestHeader('Content-type', 'application/json');
  57. request.send(JSON.stringify(data));
  58. } else {
  59. request.send();
  60. }
  61. return request;
  62. },
  63. });
  64. L.geoJSONLayer = function (options) {
  65. return new L.GeoJSONLayer(options);
  66. };
  67. }).call(this);