I have a feature that allows a user to draw a square or rectangle on an OpenLayers map. I would like to change the style of the cursor. The cursor is, by default, a blue circle. I would like to change it to a square so the symbology matches the shape that the user may create.
The solution involves adding a style attribute. I need the specifics of how to implement the style attribute for a non-image cursor that is like the default blue circle but instead, a square. Thanks!
$scope.drawBoundingBox = () => {
const bbVector = new ol.source.Vector({ wrapX: false });
const vector = new ol.layer.Vector({
source: bbVector
});
bbVector.on("addfeature", evt => {
$scope.coords = evt.feature.getGeometry().getCoordinates();
});
const style = new ol.style.Style({
stroke: new ol.style.Stroke({
color: "#FFF",
width: 3
}),
fill: new ol.style.Fill({
color: [255, 255, 255, 0]
})
});
const geometryFunction = ol.interaction.Draw.createRegularPolygon(4);
draw = new ol.interaction.Draw({
source: bbVector,
type: "Circle",
geometryFunction
});
vector.set("name", "boundingBox");
vector.setStyle(style);
map.addLayer(vector);
map.addInteraction(draw);
};
Here is a working solution that changes the default blue circle cursor to a square and allows the user to create a square or rectangle shape on the map.
$scope.drawBoundingBox = () => {
const bbVector = new ol.source.Vector({ wrapX: false });
const vector = new ol.layer.Vector({
source: bbVector
});
bbVector.on("addfeature", evt => {
$scope.coords = evt.feature.getGeometry().getCoordinates();
});
const geometryFunction = ol.interaction.Draw.createRegularPolygon(4);
draw = new ol.interaction.Draw({
source: bbVector,
type: "Circle",
geometryFunction: geometryFunction,
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: "#FFF",
width: 3
}),
fill: new ol.style.Fill({
color: [255, 255, 255, 0]
}),
geometryFunction,
image: new ol.style.RegularShape({
fill: new ol.style.Fill({
color: '#FFF'
}),
stroke: new ol.style.Stroke({
color: "blue",
width: 3
}),
points: 4,
radius: 10,
angle: Math.PI / 4
}),
}),
});
vector.set("name", "boundingBox");
map.addLayer(vector);
map.addInteraction(draw);
};