Skip to content

Commit

Permalink
Add getLayers and test it. fixes #222
Browse files Browse the repository at this point in the history
  • Loading branch information
danzel committed Oct 20, 2013
1 parent 2622819 commit 1ac2d9e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions spec/index.html
Expand Up @@ -46,6 +46,7 @@
<script type="text/javascript" src="suites/eachLayerSpec.js"></script>
<script type="text/javascript" src="suites/eventsSpec.js"></script>
<script type="text/javascript" src="suites/getBoundsSpec.js"></script>
<script type="text/javascript" src="suites/getLayersSpec.js"></script>

<script type="text/javascript" src="suites/NonPointSpec.js"></script>

Expand Down
48 changes: 48 additions & 0 deletions spec/suites/getLayersSpec.js
@@ -0,0 +1,48 @@
describe('getLayers', function () {
var map, div;
beforeEach(function () {
div = document.createElement('div');
div.style.width = '200px';
div.style.height = '200px';
document.body.appendChild(div);

map = L.map(div, { maxZoom: 18 });

map.fitBounds(new L.LatLngBounds([
[1, 1],
[2, 2]
]));
});
afterEach(function () {
document.body.removeChild(div);
});

it('hits polygons and markers before adding to map', function () {
var group = new L.MarkerClusterGroup();
var polygon = new L.Polygon([[1.5, 1.5], [2.0, 1.5], [2.0, 2.0], [1.5, 2.0]]);
var marker = new L.Marker([1.5, 1.5]);

group.addLayers([polygon, marker]);

var layers = group.getLayers();

expect(layers.length).to.be(2);
expect(layers).to.contain(marker);
expect(layers).to.contain(polygon);
});

it('hits polygons and markers after adding to map', function () {
var group = new L.MarkerClusterGroup();
var polygon = new L.Polygon([[1.5, 1.5], [2.0, 1.5], [2.0, 2.0], [1.5, 2.0]]);
var marker = new L.Marker([1.5, 1.5]);

group.addLayers([polygon, marker]);
map.addLayer(group);

var layers = group.getLayers();

expect(layers.length).to.be(2);
expect(layers).to.contain(marker);
expect(layers).to.contain(polygon);
});
});
9 changes: 9 additions & 0 deletions src/MarkerClusterGroup.js
Expand Up @@ -308,6 +308,15 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
this._nonPointGroup.eachLayer(method, context);
},

//Overrides LayerGroup.getLayers
getLayers: function () {
var layers = [];
this.eachLayer(function (l) {
layers.push(l);
});
return layers;
},

//Returns true if the given layer is in this MarkerClusterGroup
hasLayer: function (layer) {
if (!layer) {
Expand Down

0 comments on commit 1ac2d9e

Please sign in to comment.