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
| import Feature from 'ol/Feature.js'; import Map from 'ol/Map.js'; import Point from 'ol/geom/Point.js'; import Select from 'ol/interaction/Select.js'; import Stamen from 'ol/source/Stamen.js'; import VectorSource from 'ol/source/Vector.js'; import View from 'ol/View.js'; import {Icon, Style} from 'ol/style.js'; import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer.js';
function createStyle(src, img) { return new Style({ image: new Icon({ anchor: [0.5, 0.96], crossOrigin: 'anonymous', src: src, img: img, imgSize: img ? [img.width, img.height] : undefined, }), }); }
const iconFeature = new Feature(new Point([0, 0])); iconFeature.set('style', createStyle('data/icon.png', undefined));
const map = new Map({ layers: [ new TileLayer({ source: new Stamen({layer: 'watercolor'}), }), new VectorLayer({ style: function (feature) { return feature.get('style'); }, source: new VectorSource({features: [iconFeature]}), }), ], target: document.getElementById('map'), view: new View({ center: [0, 0], zoom: 3, }), });
const selectStyle = {}; const select = new Select({ style: function (feature) { const image = feature.get('style').getImage().getImage(); if (!selectStyle[image.src]) { const canvas = document.createElement('canvas'); const context = canvas.getContext('2d'); canvas.width = image.width; canvas.height = image.height; context.drawImage(image, 0, 0, image.width, image.height); const imageData = context.getImageData(0, 0, canvas.width, canvas.height); const data = imageData.data; for (let i = 0, ii = data.length; i < ii; i = i + (i % 4 == 2 ? 2 : 1)) { data[i] = 255 - data[i]; } context.putImageData(imageData, 0, 0); selectStyle[image.src] = createStyle(undefined, canvas); } return selectStyle[image.src]; }, }); map.addInteraction(select);
map.on('pointermove', function (evt) { map.getTargetElement().style.cursor = map.hasFeatureAtPixel(evt.pixel) ? 'pointer' : ''; });
|