Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getLayersOrder() to Map and Style #3279

Merged
merged 4 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## main

### ✨ Features and improvements

- Add getLayersOrder() to Map and Style ([#3279](https://github.com/maplibre/maplibre-gl-js/pull/3279))
- _...Add new stuff here..._

### 🐞 Bug fixes
Expand Down
30 changes: 30 additions & 0 deletions src/style/style.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2155,6 +2155,36 @@ describe('Style#setLayerZoomRange', () => {
});
});

describe('Style#getLayersOrder', () => {
test('returns ids of layers in the correct order', done => {
const style = new Style(getStubMap());
style.loadJSON({
'version': 8,
'sources': {
'raster': {
type: 'raster',
tiles: ['http://tiles.server']
}
},
'layers': [{
'id': 'raster',
'type': 'raster',
'source': 'raster'
}]
});

style.on('style.load', () => {
style.addLayer({
id: 'custom',
type: 'custom',
render() {}
}, 'raster');
expect(style.getLayersOrder()).toEqual(['custom', 'raster']);
done();
});
});
});

describe('Style#queryRenderedFeatures', () => {

let style;
Expand Down
11 changes: 10 additions & 1 deletion src/style/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,16 @@ export class Style extends Evented {
}

/**
* checks if a specific layer is present within the style.
* Return the ids of all layers currently in the style, including custom layers, in order.
*
* @returns ids of layers, in order
*/
getLayersOrder(): string[] {
return [...this._order];
}

/**
* Checks if a specific layer is present within the style.
*
* @param id - the id of the desired layer
* @returns a boolean specifying if the given layer is present
Expand Down
30 changes: 30 additions & 0 deletions src/ui/map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,36 @@ describe('Map', () => {
expect(mapLayer.source).toBe(layer.source);
});

describe('#getLayersOrder', () => {
test('returns ids of layers in the correct order', done => {
const map = createMap({
style: extend(createStyle(), {
'sources': {
'raster': {
type: 'raster',
tiles: ['http://tiles.server']
}
},
'layers': [{
'id': 'raster',
'type': 'raster',
'source': 'raster'
}]
})
});

map.on('style.load', () => {
map.addLayer({
id: 'custom',
type: 'custom',
render() {}
}, 'raster');
expect(map.getLayersOrder()).toEqual(['custom', 'raster']);
done();
});
});
});

describe('#resize', () => {
test('sets width and height from container clients', () => {
const map = createMap(),
Expand Down
14 changes: 14 additions & 0 deletions src/ui/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2480,6 +2480,20 @@ export class Map extends Camera {
return this.style.getLayer(id);
}

/**
* Return the ids of all layers currently in the style, including custom layers, in order.
*
* @returns ids of layers, in order
neodescis marked this conversation as resolved.
Show resolved Hide resolved
*
* @example
* ```ts
* const orderedLayerIds = map.getLayersOrder();
* ```
*/
getLayersOrder(): string[] {
return this.style.getLayersOrder();
}

/**
* Sets the zoom extent for the specified style layer. The zoom extent includes the
* [minimum zoom level](https://maplibre.org/maplibre-style-spec/layers/#minzoom)
Expand Down