Implement simplestyle for GeoJSON

This commit is contained in:
Manuel Fuhr 2023-04-20 21:45:15 +02:00
parent 0e462aac86
commit 4e719daee1
2 changed files with 17 additions and 3 deletions

View file

@ -349,7 +349,7 @@ BR.LayersConfig = L.Class.extend({
}, },
createGeoJsonLayer: function (props) { createGeoJsonLayer: function (props) {
const layer = L.geoJSON(undefined); const layer = L.geoJSON(undefined, BR.Track.getGeoJsonOptions());
fetch(props.url).then(async (response) => { fetch(props.url).then(async (response) => {
const geojson = await response.json(); const geojson = await response.json();
layer.addData(geojson); layer.addData(geojson);

View file

@ -10,13 +10,27 @@ BR.Track = {
* @returns {Object} to pass as `options` parameter to `L.geoJson` * @returns {Object} to pass as `options` parameter to `L.geoJson`
*/ */
getGeoJsonOptions: function (layersControl) { getGeoJsonOptions: function (layersControl) {
// https://github.com/mapbox/simplestyle-spec/tree/master/1.1.0
const styleMapping = [
['stroke', 'color'],
['stroke-width', 'weight'],
['stroke-opacity', 'opacity'],
['fill', 'fillColor'],
['fill-opacity', 'fillOpacity'],
];
return { return {
style: function (geoJsonFeature) { style: function (geoJsonFeature) {
var currentLayerId = layersControl.getActiveBaseLayer().layer.id; var currentLayerId = layersControl?.getActiveBaseLayer().layer.id;
return { const featureStyle = {
color: currentLayerId === 'cyclosm' ? 'yellow' : 'blue', color: currentLayerId === 'cyclosm' ? 'yellow' : 'blue',
weight: 4, weight: 4,
}; };
for (const [simpleStyle, leafletStyle] of styleMapping) {
if (geoJsonFeature?.properties?.[simpleStyle]) {
featureStyle[leafletStyle] = geoJsonFeature.properties[simpleStyle];
}
}
return featureStyle;
}, },
interactive: false, interactive: false,
filter: function (geoJsonFeature) { filter: function (geoJsonFeature) {