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
| const mapInfo = { map: null, extent: null, projection: null, setProjection: function (w, h) { this.extent = [0, 0, w, h]; this.projection = new ol.proj.Projection({ code: 'pixel', units: 'pixels', extent: this.extent, }); }, init: function (imageName) { const view = new ol.View({ projection: this.projection, center: ol.extent.getCenter(this.extent), zoom: 2, maxZoom: 8, });
const imageLayer = new ol.layer.Image({ source: new ol.source.ImageStatic({ url: 'images/' + imageName, imageSize: [this.extent[2], this.extent[3]], projection: this.projection, imageExtent: this.extent, }), });
this.map = new ol.Map({ target: 'map', layers: [imageLayer], view: view, });
this.zoomFit(); }, zoomFit: function () { this.map.getView().fit(this.extent, { duration: 200 }); }, addVectorLayer: function (x, y) { const iconFeature = new ol.Feature({ geometry: new ol.geom.Point([x, y]), type: 'icon', name: 'icon', });
const vectorLayer = new ol.layer.Vector({ source: new ol.source.Vector({ features: [iconFeature], wrapX: false, }), style: function (feature) { return styles[feature.get('type')]; }, });
this.map.addLayer(vectorLayer); }, };
document.addEventListener('DOMContentLoaded', function () { const width = 500; const height = 500; const imageName = 'image.png';
mapInfo.setProjection(width, height); mapInfo.init(imageName);
mapInfo.addVectorLayer(100, 100); });
|