LayerGroup.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import {Layer} from './Layer';
  2. import * as Util from '../core/Util';
  3. /*
  4. * @class LayerGroup
  5. * @aka L.LayerGroup
  6. * @inherits Interactive layer
  7. *
  8. * Used to group several layers and handle them as one. If you add it to the map,
  9. * any layers added or removed from the group will be added/removed on the map as
  10. * well. Extends `Layer`.
  11. *
  12. * @example
  13. *
  14. * ```js
  15. * L.layerGroup([marker1, marker2])
  16. * .addLayer(polyline)
  17. * .addTo(map);
  18. * ```
  19. */
  20. export var LayerGroup = Layer.extend({
  21. initialize: function (layers, options) {
  22. Util.setOptions(this, options);
  23. this._layers = {};
  24. var i, len;
  25. if (layers) {
  26. for (i = 0, len = layers.length; i < len; i++) {
  27. this.addLayer(layers[i]);
  28. }
  29. }
  30. },
  31. // @method addLayer(layer: Layer): this
  32. // Adds the given layer to the group.
  33. addLayer: function (layer) {
  34. var id = this.getLayerId(layer);
  35. this._layers[id] = layer;
  36. if (this._map) {
  37. this._map.addLayer(layer);
  38. }
  39. return this;
  40. },
  41. // @method removeLayer(layer: Layer): this
  42. // Removes the given layer from the group.
  43. // @alternative
  44. // @method removeLayer(id: Number): this
  45. // Removes the layer with the given internal ID from the group.
  46. removeLayer: function (layer) {
  47. var id = layer in this._layers ? layer : this.getLayerId(layer);
  48. if (this._map && this._layers[id]) {
  49. this._map.removeLayer(this._layers[id]);
  50. }
  51. delete this._layers[id];
  52. return this;
  53. },
  54. // @method hasLayer(layer: Layer): Boolean
  55. // Returns `true` if the given layer is currently added to the group.
  56. // @alternative
  57. // @method hasLayer(id: Number): Boolean
  58. // Returns `true` if the given internal ID is currently added to the group.
  59. hasLayer: function (layer) {
  60. var layerId = typeof layer === 'number' ? layer : this.getLayerId(layer);
  61. return layerId in this._layers;
  62. },
  63. // @method clearLayers(): this
  64. // Removes all the layers from the group.
  65. clearLayers: function () {
  66. return this.eachLayer(this.removeLayer, this);
  67. },
  68. // @method invoke(methodName: String, …): this
  69. // Calls `methodName` on every layer contained in this group, passing any
  70. // additional parameters. Has no effect if the layers contained do not
  71. // implement `methodName`.
  72. invoke: function (methodName) {
  73. var args = Array.prototype.slice.call(arguments, 1),
  74. i, layer;
  75. for (i in this._layers) {
  76. layer = this._layers[i];
  77. if (layer[methodName]) {
  78. layer[methodName].apply(layer, args);
  79. }
  80. }
  81. return this;
  82. },
  83. onAdd: function (map) {
  84. this.eachLayer(map.addLayer, map);
  85. },
  86. onRemove: function (map) {
  87. this.eachLayer(map.removeLayer, map);
  88. },
  89. // @method eachLayer(fn: Function, context?: Object): this
  90. // Iterates over the layers of the group, optionally specifying context of the iterator function.
  91. // ```js
  92. // group.eachLayer(function (layer) {
  93. // layer.bindPopup('Hello');
  94. // });
  95. // ```
  96. eachLayer: function (method, context) {
  97. for (var i in this._layers) {
  98. method.call(context, this._layers[i]);
  99. }
  100. return this;
  101. },
  102. // @method getLayer(id: Number): Layer
  103. // Returns the layer with the given internal ID.
  104. getLayer: function (id) {
  105. return this._layers[id];
  106. },
  107. // @method getLayers(): Layer[]
  108. // Returns an array of all the layers added to the group.
  109. getLayers: function () {
  110. var layers = [];
  111. this.eachLayer(layers.push, layers);
  112. return layers;
  113. },
  114. // @method setZIndex(zIndex: Number): this
  115. // Calls `setZIndex` on every layer contained in this group, passing the z-index.
  116. setZIndex: function (zIndex) {
  117. return this.invoke('setZIndex', zIndex);
  118. },
  119. // @method getLayerId(layer: Layer): Number
  120. // Returns the internal ID for a layer
  121. getLayerId: function (layer) {
  122. return Util.stamp(layer);
  123. }
  124. });
  125. // @factory L.layerGroup(layers?: Layer[], options?: Object)
  126. // Create a layer group, optionally given an initial set of layers and an `options` object.
  127. export var layerGroup = function (layers, options) {
  128. return new LayerGroup(layers, options);
  129. };