|
1 | | -import TileLayer from 'ol/layer/Tile' |
2 | | -import { XYZ } from 'ol/source' |
| 1 | +import type { FeatureLike } from 'ol/Feature' |
| 2 | +import VectorTileLayer from 'ol/layer/VectorTile' |
| 3 | +import { applyStyle } from 'ol-mapbox-style' |
| 4 | + |
| 5 | +const POSITRON_STYLE_URL = 'https://basemaps.cartocdn.com/gl/positron-gl-style/style.json' |
| 6 | + |
| 7 | +// Only render administrative boundaries and place/water/road labels |
| 8 | +// (no background, fills, or road geometry — keeps the layer transparent) |
| 9 | +const LABEL_AND_BOUNDARY_LAYERS = [ |
| 10 | + 'boundary_county', |
| 11 | + 'boundary_state', |
| 12 | + 'boundary_country_outline', |
| 13 | + 'boundary_country_inner', |
| 14 | + 'waterway_label', |
| 15 | + 'watername_ocean', |
| 16 | + 'watername_sea', |
| 17 | + 'watername_lake', |
| 18 | + 'watername_lake_line', |
| 19 | + 'place_hamlet', |
| 20 | + 'place_suburbs', |
| 21 | + 'place_villages', |
| 22 | + 'place_town', |
| 23 | + 'place_country_2', |
| 24 | + 'place_country_1', |
| 25 | + 'place_state', |
| 26 | + 'place_continent', |
| 27 | + 'place_city_r6', |
| 28 | + 'place_city_r5', |
| 29 | + 'place_city_dot_r7', |
| 30 | + 'place_city_dot_r4', |
| 31 | + 'place_city_dot_r2', |
| 32 | + 'place_city_dot_z7', |
| 33 | + 'place_capital_dot_z7', |
| 34 | + 'roadname_minor', |
| 35 | + 'roadname_sec', |
| 36 | + 'roadname_pri', |
| 37 | + 'roadname_major', |
| 38 | +] |
3 | 39 |
|
4 | 40 | export default function createLabelLayer() { |
5 | | - return new TileLayer({ |
6 | | - source: new XYZ({ |
7 | | - url: 'https://{a-d}.basemaps.cartocdn.com/dark_only_labels/{z}/{x}/{y}@2x.png', |
8 | | - maxZoom: 20, |
9 | | - minZoom: 0, |
10 | | - crossOrigin: 'anonymous', |
11 | | - attributions: |
12 | | - '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>, © <a href="https://carto.com/attributions">CARTO</a>', |
13 | | - }), |
| 41 | + const layer = new VectorTileLayer({ declutter: true }) |
| 42 | + |
| 43 | + applyStyle(layer, POSITRON_STYLE_URL, { layers: LABEL_AND_BOUNDARY_LAYERS }).then(() => { |
| 44 | + const originalStyleFn = layer.getStyleFunction()! |
| 45 | + layer.setStyle((feature: FeatureLike, resolution: number) => { |
| 46 | + const styles = originalStyleFn(feature, resolution) |
| 47 | + if (!styles) return styles |
| 48 | + const arr = Array.isArray(styles) ? styles : [styles] |
| 49 | + for (const style of arr) { |
| 50 | + const text = style.getText() |
| 51 | + if (text) { |
| 52 | + text.getFill()?.setColor('#ffffff') |
| 53 | + text.getStroke()?.setColor('#333333') |
| 54 | + } |
| 55 | + } |
| 56 | + return arr |
| 57 | + }) |
14 | 58 | }) |
| 59 | + |
| 60 | + return layer |
15 | 61 | } |
0 commit comments