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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| import Collection from 'ol/Collection.js'; import Feature from 'ol/Feature.js'; import GeoJSON from 'ol/format/GeoJSON.js'; import Map from 'ol/Map.js'; import Point from 'ol/geom/Point.js'; import View from 'ol/View.js'; import { Circle as CircleStyle, Fill, Stroke, Style, Text, } from 'ol/style.js'; import {OSM, Vector as VectorSource} from 'ol/source.js'; import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer.js';
const features = [ { geometry: new Point([-8300000, 6095000]), textAlign: 'left', }, { geometry: new Point([-8150000, 6095000]), textAlign: 'center', }, { geometry: new Point([-8000000, 6095000]), textAlign: 'right', }, { geometry: new Point([-8300000, 6025000]), textAlign: 'left', justify: 'center', }, { geometry: new Point([-8150000, 6025000]), textAlign: 'center', justify: 'center', }, { geometry: new Point([-8000000, 6025000]), textAlign: 'right', justify: 'center', }, { geometry: new Point([-8300000, 5955000]), textAlign: 'left', justify: 'left', }, { geometry: new Point([-8150000, 5955000]), textAlign: 'center', justify: 'left', }, { geometry: new Point([-8000000, 5955000]), textAlign: 'right', justify: 'left', }, ];
function createStyle({textAlign, justify}) { return new Style({ image: new CircleStyle({ radius: 10, fill: new Fill({color: 'rgba(255, 0, 0, 0.1)'}), stroke: new Stroke({color: 'red', width: 1}), }), text: new Text({ font: '16px sans-serif', textAlign, justify, text: `Justify text inside box\ntextAlign: ${textAlign}` + (justify ? `\njustify: ${justify}` : ''), fill: new Fill({ color: [255, 255, 255, 1], }), backgroundFill: new Fill({ color: [168, 50, 153, 0.6], }), padding: [2, 2, 2, 2], }), }); }
const vectorPoints = new VectorLayer({ source: new VectorSource({ features: new Collection( features.map((featureOptions) => { const feature = new Feature({ geometry: featureOptions.geometry, }); feature.setStyle(createStyle(featureOptions)); return feature; }) ), format: new GeoJSON(), }), });
const map = new Map({ layers: [ new TileLayer({ source: new OSM(), }), vectorPoints, ], target: 'map', view: new View({ center: [-8150000, 6025000], zoom: 8, }), });
|