I'm a bit of an OpenLayers newbie and I'm using v3.0.0.
I've recently added a style function code to my map to show labels on features, using some code I found online:
function labelStyle(feature, resolution) {
var text = '';
if (options.showLabels && (resolution < 80)) {
text = feature.get('name');
}
if (!styleCache[text]) {
styleCache[text] = [
new window.ol.style.Style({
stroke: new window.ol.style.Stroke({ color: [0, 153, 255, 1], width: 2.5 }),
fill: new window.ol.style.Fill({ color: [0, 255, 0, 0.1] }),
text: new window.ol.style.Text({
font: '16px Calibri,sans-serif',
text: text,
fill: new ol.style.Fill({
color: [255, 255, 255, 1]
}),
stroke: new ol.style.Stroke({
color: [0, 0, 0, 1],
width: 3
})
})
})
];
}
return styleCache[text];
}
This works fine, I can pass this function instead of a style when creating the vector layer and labels now appear in the centre of the features. However, if I select a feature, the label disappears.
So my aim is to be able to apply this style to selected features.
I've seen the code for a StyleMap, where you can define styles for default, select, etc. But if I implement this StyleMap, I lose the ability to get the name from the feature to use for labelling.
It seems I can have one but not the other.