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-tile-layer.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* global L */
  2. (function() {
  3. L.GeoJSONTileLayer = L.GridLayer.extend({
  4. includes: L.Evented.prototype,
  5. url: null,
  6. map: null,
  7. layer: null,
  8. features: null,
  9. cache: null,
  10. //
  11. // Leaflet layer methods
  12. //
  13. initialize(url, options) {
  14. this.url = url;
  15. this.layer = new L.GeoJSON(null, options);
  16. this.features = {};
  17. this.cache = {};
  18. L.GridLayer.prototype.initialize.call(this, options);
  19. },
  20. createTile(coords, done) {
  21. var tile = L.DomUtil.create('div', 'leaflet-tile');
  22. tile.style['box-shadow'] = 'inset 0 0 2px #f00';
  23. var url = this._expandUrl(this.url, coords);
  24. if (this.cache[coords]) {
  25. done.call(this);
  26. } else {
  27. this._ajaxRequest('GET', url, false, this._updateCache.bind(this, done, coords));
  28. }
  29. return tile;
  30. },
  31. onAdd(map) {
  32. L.GridLayer.prototype.onAdd.call(this, map);
  33. map.addLayer(this.layer);
  34. this.map = map;
  35. map.on('zoomanim', this._onZoomAnim.bind(this));
  36. this.on('loading', this._onLoading.bind(this));
  37. this.on('tileload', this._onTileLoad.bind(this));
  38. this.on('tileunload', this._onTileUnLoad.bind(this));
  39. },
  40. onRemove(map) {
  41. this.off('tileunload', this._onTileUnLoad.bind(this));
  42. this.off('tileload', this._onTileLoad.bind(this));
  43. this.off('loading', this._onLoading.bind(this));
  44. map.off('zoomanim', this._onZoomAnim.bind(this));
  45. this.map = null;
  46. map.removeLayer(this.layer)
  47. L.GridLayer.prototype.onRemove.call(this, map);
  48. },
  49. //
  50. // Custom methods
  51. //
  52. _expandUrl: function(template, coords) {
  53. return L.Util.template(template, coords);
  54. },
  55. _updateTiles: function() {
  56. this.layer.clearLayers();
  57. this.features = {};
  58. for (var coords in this.cache) {
  59. if (this.cache.hasOwnProperty(coords)) {
  60. this._drawTile(coords);
  61. }
  62. }
  63. },
  64. _drawTile(coords) {
  65. var geoData = this.cache[coords];
  66. if (geoData.type == 'FeatureCollection'){
  67. geoData = geoData.features;
  68. }
  69. for (var i=0;i<geoData.length;i++) {
  70. var id = geoData[i].id;
  71. if (!this.features[id]) {
  72. this.layer.addData(geoData[i]);
  73. this.features[id] = true;
  74. }
  75. }
  76. if (!this.cache[coords]) {
  77. this.cache[coords] = geoData;
  78. }
  79. },
  80. _updateCache: function(done, coords, geoData) {
  81. this.cache[coords] = geoData;
  82. done.call(this);
  83. },
  84. _ajaxRequest: function(method, url, data, callback) {
  85. var request = new XMLHttpRequest();
  86. request.open(method, url, true);
  87. request.onreadystatechange = function() {
  88. if (request.readyState === 4 && request.status === 200) {
  89. callback(JSON.parse(request.responseText));
  90. }
  91. };
  92. if (data) {
  93. request.setRequestHeader('Content-type', 'application/json');
  94. request.send(JSON.stringify(data));
  95. } else {
  96. request.send();
  97. }
  98. return request;
  99. },
  100. _onZoomAnim: function (e) {
  101. var zoom = e.zoom;
  102. if ((this.options.maxZoom && zoom > this.options.maxZoom) ||
  103. (this.options.minZoom && zoom < this.options.minZoom)) {
  104. this.map.removeLayer(this.layer);
  105. this.cache = {};
  106. this.layer.clearLayers();
  107. } else {
  108. this._updateTiles();
  109. this.map.addLayer(this.layer);
  110. }
  111. },
  112. _onLoading: function (e) {
  113. this._updateTiles();
  114. },
  115. _onTileLoad: function (e) {
  116. this._drawTile(e.coords);
  117. },
  118. _onTileUnLoad: function (e) {
  119. delete this.cache[e.coords]
  120. },
  121. });
  122. L.geoJSONTileLayer = function (url, options) {
  123. return new L.GeoJSONTileLayer(url, options);
  124. };
  125. })();