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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
| import Feature from 'ol/Feature.js'; import Map from 'ol/Map.js'; import Point from 'ol/geom/Point.js'; import TileJSON from 'ol/source/TileJSON.js'; import VectorSource from 'ol/source/Vector.js'; import View from 'ol/View.js'; import {Circle, Fill, Icon, Stroke, Style, Text} from 'ol/style.js'; import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer.js';
const iconFeature = new Feature({ geometry: new Point([0, 0]), });
const iconStyle = new Style({ image: new Icon({ anchor: [0.5, 1], src: 'data/world.png', }), text: new Text({ text: 'World\nText', font: 'bold 30px Calibri,sans-serif', fill: new Fill({ color: 'black', }), stroke: new Stroke({ color: 'white', width: 2, }), }), });
const pointStyle = new Style({ image: new Circle({ radius: 7, fill: new Fill({ color: 'black', }), stroke: new Stroke({ color: 'white', width: 2, }), }), });
iconFeature.setStyle([pointStyle, iconStyle]);
const vectorSource = new VectorSource({ features: [iconFeature], });
const vectorLayer = new VectorLayer({ source: vectorSource, });
const rasterLayer = new TileLayer({ source: new TileJSON({ url: 'https://a.tiles.mapbox.com/v3/aj.1x1-degrees.json?secure=1', crossOrigin: '', }), });
const map = new Map({ layers: [rasterLayer, vectorLayer], target: 'map', view: new View({ center: [0, 0], zoom: 3, }), });
const textAlignments = ['left', 'center', 'right']; const textBaselines = ['top', 'middle', 'bottom']; const controls = {}; const controlIds = [ 'rotation', 'rotateWithView', 'scaleX', 'scaleY', 'anchorX', 'anchorY', 'displacementX', 'displacementY', 'textRotation', 'textRotateWithView', 'textScaleX', 'textScaleY', 'textAlign', 'textBaseline', 'textOffsetX', 'textOffsetY', ]; controlIds.forEach(function (id) { const control = document.getElementById(id); const output = document.getElementById(id + 'Out'); function setOutput() { const value = parseFloat(control.value); if (control.type === 'checkbox') { output.innerText = control.checked; } else if (id === 'textAlign') { output.innerText = textAlignments[value]; } else if (id === 'textBaseline') { output.innerText = textBaselines[value]; } else { output.innerText = control.step.startsWith('0.') ? value.toFixed(2) : value; } } control.addEventListener('input', function () { setOutput(); updateStyle(); }); setOutput(); controls[id] = control; });
function updateStyle() { iconStyle .getImage() .setRotation(parseFloat(controls['rotation'].value) * Math.PI);
iconStyle.getImage().setRotateWithView(controls['rotateWithView'].checked);
iconStyle .getImage() .setScale([ parseFloat(controls['scaleX'].value), parseFloat(controls['scaleY'].value), ]);
iconStyle .getImage() .setAnchor([ parseFloat(controls['anchorX'].value), parseFloat(controls['anchorY'].value), ]);
iconStyle .getImage() .setDisplacement([ parseFloat(controls['displacementX'].value), parseFloat(controls['displacementY'].value), ]);
iconStyle .getText() .setRotation(parseFloat(controls['textRotation'].value) * Math.PI);
iconStyle.getText().setRotateWithView(controls['textRotateWithView'].checked);
iconStyle .getText() .setScale([ parseFloat(controls['textScaleX'].value), parseFloat(controls['textScaleY'].value), ]);
iconStyle .getText() .setTextAlign(textAlignments[parseFloat(controls['textAlign'].value)]);
iconStyle .getText() .setTextBaseline(textBaselines[parseFloat(controls['textBaseline'].value)]);
iconStyle.getText().setOffsetX(parseFloat(controls['textOffsetX'].value)); iconStyle.getText().setOffsetY(parseFloat(controls['textOffsetY'].value));
iconFeature.changed(); } updateStyle();
map.on('pointermove', function (e) { const hit = map.hasFeatureAtPixel(e.pixel); map.getTargetElement().style.cursor = hit ? 'pointer' : ''; });
|