TransformStyleFunction
TransformStyleFunction = (
previous:StyleSpecification|undefined,next:StyleSpecification) =>StyleSpecification
Входит в параметры Map.setStyle. transformStyle — это вспомогательная функция, которая позволяет изменить стиль после его загрузки, но до его применения к состоянию карты.
Эта функция предоставляет доступ к предыдущему и следующему стилям и обычно используется для поддержки различных сценариев, например:
- если предыдущий стиль содержит определённое «состояние», которое нужно корректно перенести в новый стиль;
- если желаемый стиль представляет собой комбинацию предыдущего и нового стилей;
- если новый стиль требует изменений в зависимости от внешнего состояния;
- если в новом стиле используются относительные пути, которые нужно преобразовать в абсолютные.
Параметры
| Параметр | Тип | Описание |
|---|---|---|
previous |
StyleSpecification | undefined
| Текущий стиль. |
next | StyleSpecification | Следующий стиль. |
Возвращает
StyleSpecification
стиль, который будет применён к карте
Пример
map.setStyle('https://demotiles.maplibre.org/style.json', {
transformStyle: (previousStyle, nextStyle) => ({
...nextStyle,
// make relative sprite path like "../sprite" absolute
sprite: new URL(nextStyle.sprite, "https://demotiles.maplibre.org/styles/osm-bright-gl-style/sprites/").href,
// make relative glyphs path like "../fonts/{fontstack}/{range}.pbf" absolute
glyphs: new URL(nextStyle.glyphs, "https://demotiles.maplibre.org/font/").href,
sources: {
// make relative vector url like "../../" absolute
...nextStyle.sources.map(source => {
if (source.url) {
source.url = new URL(source.url, "https://tiles.openfreemap.org/planet");
}
return source;
}),
// copy a source from previous style
'osm': previousStyle.sources.osm
},
layers: [
// background layer
nextStyle.layers[0],
// copy a layer from previous style
previousStyle.layers[0],
// other layers from the next style
...nextStyle.layers.slice(1).map(layer => {
// hide the layers we don't need from demotiles style
if (layer.id.startsWith('geolines')) {
layer.layout = {...layer.layout || {}, visibility: 'none'};
// filter out US polygons
} else if (layer.id.startsWith('coastline') || layer.id.startsWith('countries')) {
layer.filter = ['!=', ['get', 'ADM0_A3'], 'USA'];
}
return layer;
})
]
})
});
© MapLibre contributors
Licensed under the 3-Clause BSD License.
https://maplibre.org/maplibre-gl-js/docs/API/type-aliases/TransformStyleFunction/