1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| import MVT from 'ol/format/MVT.js'; import Map from 'ol/Map.js'; import VectorTile from 'ol/layer/VectorTile.js'; import VectorTileSource from 'ol/source/VectorTile.js'; import View from 'ol/View.js'; import WebGLVectorTileLayerRenderer from 'ol/renderer/webgl/VectorTileLayer.js'; import {Fill, Icon, Stroke, Style, Text} from 'ol/style.js'; import {asArray} from 'ol/color.js'; import {packColor, parseLiteralStyle} from 'ol/webgl/styleparser.js';
const key = 'Your Mapbox access token from https://mapbox.com/ here';
const result = parseLiteralStyle({ 'fill-color': ['get', 'fillColor'], 'stroke-color': ['get', 'strokeColor'], 'stroke-width': ['get', 'strokeWidth'], 'circle-radius': 4, 'circle-fill-color': '#777', });
class WebGLVectorTileLayer extends VectorTile { createRenderer() { return new WebGLVectorTileLayerRenderer(this, { style: { fill: { fragment: result.builder.getFillFragmentShader(), vertex: result.builder.getFillVertexShader(), }, stroke: { fragment: result.builder.getStrokeFragmentShader(), vertex: result.builder.getStrokeVertexShader(), }, symbol: { fragment: result.builder.getSymbolFragmentShader(), vertex: result.builder.getSymbolVertexShader(), }, attributes: { fillColor: { size: 2, callback: (feature) => { const style = this.getStyle()(feature, 1)[0]; const color = asArray(style?.getFill()?.getColor() || '#eee'); return packColor(color); }, }, strokeColor: { size: 2, callback: (feature) => { const style = this.getStyle()(feature, 1)[0]; const color = asArray(style?.getStroke()?.getColor() || '#eee'); return packColor(color); }, }, strokeWidth: { size: 1, callback: (feature) => { const style = this.getStyle()(feature, 1)[0]; return style?.getStroke()?.getWidth() * 2 || 0; }, }, }, }, }); } }
const map = new Map({ layers: [ new WebGLVectorTileLayer({ source: new VectorTileSource({ attributions: '© <a href="https://www.mapbox.com/map-feedback/">Mapbox</a> ' + '© <a href="https://www.openstreetmap.org/copyright">' + 'OpenStreetMap contributors</a>', format: new MVT(), url: 'https://{a-d}.tiles.mapbox.com/v4/mapbox.mapbox-streets-v6/' + '{z}/{x}/{y}.vector.pbf?access_token=' + key, }), style: createMapboxStreetsV6Style(Style, Fill, Stroke, Icon, Text), }), ], target: 'map', view: new View({ center: [0, 0], zoom: 2, }), });
|