I am new to OpenLayers, I am using it for an office layout, so I use an ImageStatic layer as background, image size is 1024x968
. As it is not an earth map, I want to draw a box on map and get the real size of the box. I don't know where I can set the ratio, I mean if 1024
equals to 1024
meter, how can I set this value? Or maybe 1024
pixels equal to 2048
meter for another image, how to set that too?
Below is the code I use to create map:
const extent = [0, 0, 1024, 968];
const projection = new Projection({
code: 'mycompany',
units: 'pixels',
metersPerUnit: 10,
extent,
});
const imageLayer = new ImageLayer({
source: new ImageStatic({
url: 'http://localhost:8081/static/a35.png',
projection,
imageExtent: extent,
}),
});
const source = new Vector({ wrapX: false, features: this.features });
const vector = new VectorLayer({
source,
});
const map = new Map({
controls: Control.defaults().extend([new FullScreen()]),
layers: [imageLayer, vector],
overlays: [overlay],
target: 'map',
view: new View({
projection,
center: Extent.getCenter(extent),
zoom: 2,
minZoom: 2,
maxZoom: 4,
}),
});
map.addInteraction(draw);
this.createMeasureTooltip(map);
draw.on(
'drawstart',
evt => {
this.sketch = evt.feature;
this.listener = this.sketch.getGeometry().on('change', evtChange => {
this.measureTooltipElement.innerHTML = this.formatArea(evtChange.target);
this.measureTooltip.setPosition(evtChange.target.getInteriorPoint().getCoordinates());
});
},
this,
);
I am using following function to get the area size:
formatArea(polygon) {
const area = Sphere.getArea(polygon);
let output;
if (area > 10000) {
output = `${Math.round(area / 1000000 * 100) / 100} km<sup>2</sup>`;
} else {
output = `${Math.round(area * 100) / 100} m<sup>2</sup>`;
}
return output;
}
But this just called getArea
, and I don't understand how can it calculate the size in meter?
Whenever I change the metersPerUnit
to 10 or 10000, the area are always same.
I changed my code as below and by changing radius
I can get any number I want, but I don't know if it's a good way:
const area = Sphere.getArea(polygon, {
radius: 1000000,
});
Sphere
object. openlayers.org/en/latest/apidoc/ol.Sphere.html#.getArea github.com/openlayers/openlayers/blob/…