I want to convert my openlayers 2 features to openlayers 3. I have following part in openlayers 2. I need an equivalent of StyleMap in openlayers 3 so that I can distinguish the default action and select action.
var subMap = new OpenLayers.StyleMap({
"default": new OpenLayers.Style({
fillColor:"orange",
strokeColor:"orange"
}),
"select": new OpenLayers.Style({
fillColor:"#00FFFF",
strokeColor:"#00FFFF"
})
});
I can write the following for "default" case. But where to mention for the "select" case?
var subMap= new ol.style.Style({
fill: new ol.style.Fill({
color: "orange"
}),
stroke: new ol.style.Stroke({
color:"orange"
})
});
Can anyone please help in this regard?
Answer
In OpenLayers 3 (or 4), features selection is handled by an interaction. The relevant doc about selection is here: http://openlayers.org/en/latest/apidoc/ol.interaction.Select.html
Here's a piece of code that allows to select any features in your map, and to apply a special style to it (styleSelect
):
// render selected features with a red stroke
var styleSelect = new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'red',
width: 2
})
});
// a normal select interaction to handle click
var select = new ol.interaction.Select({style: styleSelect});
map.addInteraction(select);
The default (not selected) style can be defined elsewhere, using another ol.style.Style
object.
No comments:
Post a Comment