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

Display simple legend next to layer title as QGIS does #3641

Closed
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
17 changes: 17 additions & 0 deletions assets/src/legacy/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -5464,6 +5464,23 @@ window.lizMap = function() {
self.events.triggerEvent("layersadded", self);


// Add legends
const wms = new WMS();
const layersName = layers.map(layer => layer.params.LAYERS).join();
const wmsParams = {
LAYERS: layersName,
};

wms.getLegendGraphics(wmsParams).then(response => {
const treeviewLayers = document.querySelectorAll('.liz-layer');
treeviewLayers.forEach((treeviewLayer, index) => {
const icon = response.nodes[index].icon;
if(icon){
treeviewLayer.querySelector('button').insertAdjacentHTML('afterend',`<img class="legend" src="data:image/png;base64, ${icon}">`);
}
});
});

// Verifying z-index
var lastLayerZIndex = map.layers[map.layers.length - 1].getZIndex();
if (lastLayerZIndex > map.Z_INDEX_BASE['Feature'] - 100) {
Expand Down
25 changes: 25 additions & 0 deletions assets/src/modules/WMS.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ export default class WMS {
SRS: 'EPSG:4326',
INFO_FORMAT: 'text/html'
};

this._defaultGetLegendGraphicsParameters = {
repository: lizUrls.params.repository,
project: lizUrls.params.project,
SERVICE: 'WMS',
REQUEST: 'GetLegendGraphics',
VERSION: '1.3.0',
FORMAT: 'application/json',
};
}

/**
Expand All @@ -27,4 +36,20 @@ export default class WMS {
});
return await response.text();
}

/**
* @param {Object} options - optional parameters which can override this._defaultGetLegendGraphicsParameters
* @return {Promise} Promise object represents data
* @memberof WMS
*/
async getLegendGraphics(options) {
const response = await fetch(lizUrls.wms, {
method: "POST",
body: new URLSearchParams({
...this._defaultGetLegendGraphicsParameters,
...options
})
});
return await response.json();
}
}
3 changes: 3 additions & 0 deletions lizmap/www/assets/css/map.css
Original file line number Diff line number Diff line change
Expand Up @@ -1663,6 +1663,9 @@ lizmap-print .flex {
position: relative;
max-width: none;
}
#switcher img.legend {
margin-left: 5px;
}
#switcher .treeTable td a.expander {
font-size : 1em;
}
Expand Down
Loading
Loading