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 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* global L */
  2. (function() {
  3. L.GeoJSONTileLayer = L.GridLayer.extend({
  4. includes: L.Evented.prototype,
  5. url: null,
  6. layer: null,
  7. features: null,
  8. cache: null,
  9. initialize(url, options) {
  10. this.url = url;
  11. this.layer = new L.GeoJSON(null, options);
  12. this.features = {};
  13. this.cache = {};
  14. L.GridLayer.prototype.initialize.call(this, options);
  15. },
  16. createTile: function (coords) {
  17. var tile = L.DomUtil.create('div', 'leaflet-tile');
  18. tile.style['box-shadow'] = 'inset 0 0 2px #f00';
  19. var url = L.Util.template(this.url, coords);
  20. if (this.cache[url]) {
  21. this.updateLayers(url, this.cache[url]);
  22. } else {
  23. this.ajaxRequest('GET', url, false, this.updateLayers.bind(this, url));
  24. }
  25. return tile;
  26. },
  27. updateLayers: function(url, geoData) {
  28. if (geoData.type == 'FeatureCollection'){
  29. for (var i=0;i<geoData.features.length;i++) {
  30. var id = geoData.features[i].id;
  31. if (!this.features[id]) {
  32. this.layer.addData(geoData.features[i]);
  33. this.features[id] = true;
  34. }
  35. }
  36. }
  37. if (!this.cache[url]) {
  38. this.cache[url] = geoData;
  39. }
  40. },
  41. onAdd(map) {
  42. L.GridLayer.prototype.onAdd.call(this, map);
  43. map.addLayer(this.layer);
  44. this.map = map;
  45. },
  46. onRemove(map) {
  47. this.map = null;
  48. map.removeLayer(this.layer)
  49. L.GridLayer.prototype.onRemove.call(this, map);
  50. },
  51. ajaxRequest: function(method, url, data, callback) {
  52. var request = new XMLHttpRequest();
  53. request.open(method, url, true);
  54. request.onreadystatechange = function() {
  55. if (request.readyState === 4 && request.status === 200) {
  56. callback(JSON.parse(request.responseText));
  57. }
  58. };
  59. if (data) {
  60. request.setRequestHeader('Content-type', 'application/json');
  61. request.send(JSON.stringify(data));
  62. } else {
  63. request.send();
  64. }
  65. return request;
  66. },
  67. });
  68. L.geoJSONTileLayer = function (url, options) {
  69. return new L.GeoJSONTileLayer(url, options);
  70. };
  71. }).call(this);