Sunday 9 February 2020

javascript - Openlayers 3 equivalent of render intents of OpenLayers.Stylemap?


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

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...