Tuesday 27 August 2019

Openlayers 3 Select Interaction style function


I have a style function for LineStrings which will place a circle at each vertex of a feature.


When I select the feature it reverts to using the default style even though I have replicated my style function with a minor (colour) change. The other minor bug is that the function has two params (start, end) but I would like to place the circle on every vertex. Is there a way to achieve this?


Specifically though, is there a reason why it might be unable to display a selected feature using a style function? I am replicating the code below:


var selectedStyle = function (feature, resolution) {
var geometry = feature.getGeometry();
var styles = [
// linestring
new ol.style.Style({

stroke: new ol.style.Stroke({
color: '#ffcc33',
width: 6
})
})
];

geometry.forEachSegment(function (start, end) {
var dx = end[0] - start[0];
var dy = end[1] - start[1];

var rotation = Math.atan2(dy, dx);
// circles
styles.push(new ol.style.Style({
geometry: new ol.geom.Point(end),
image: new ol.style.Circle({
radius: 5,
fill: new ol.style.Fill({
color: 'rgb(0, 0, 255)'
}),
stroke: new ol.style.Stroke({ color: 'blue', width: 2 })

})
}));
});

return styles;
};

Answer



UPDATE:


When a function is used on ol.Feature#setStyle your feature is referenced with the this keyword. Like:


var selectedStyle = function () {

console.info(this);
var feature = this;
var geometry = feature.getGeometry();
};

The reason that your features don't keep style is because they are (when selected) on a temporary ol.layer.Vector. So, to achieve what you want you can do:


select.on('select', function(evt){
var selected = evt.selected;
var deselected = evt.deselected;


if (selected.length) {
selected.forEach(function(feature){
console.info(feature);
feature.setStyle(style_modify);
});
} else {
deselected.forEach(function(feature){
console.info(feature);
feature.setStyle(null);
});

}
});

http://jsfiddle.net/jonataswalker/eogk4r4b/


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...