How to change styles of selected clusters on zoom in OL 4.6?










0















I have a map with a vector layer and a cluster source. I choosed to display the clusters as circles with the number of features written inside. In case the cluster contains only one feature, it displays as a custom icon. If the zoom is too important, I set the distance between clusters at 0, to cancel the clusterization (I'm aware each feature is still considered as a cluster). I also have a select interaction with a custom fonction (Kadricarto.selectEquipementStyle) to change the style. The interaction works well and the clusters selected are displayed with the right style.



However, if I zoom in or out, the selected clusters are still displayed in the initial zoom of the selection, with their initial style.



Is there an easy way to dynamically re-style the selected clusters at each zoom level without dispatching a new select event ?



I stumbled upon this link (http://jorix.github.io/OL-FeaturePopups/examples/feature-popups.html) which produces the kind of behavior I want, but it's for OL2...
This is my code:



vectorEquipementSource = new ol.source.Vector();
clusterEquipementSource = new ol.source.Cluster(
source: vectorEquipementSource,
distance: constante.DistanceCluster
);
vectorLayerEquipement = new ol.layer.Vector(
source: clusterEquipementSource,
style: vectorEquipementStyle,
maxResolution: seuilVueCarto,
minResolution: minRes,
extent: maxextent,
visible: false
);

selectEquipementStyle = function (feat, iscluster)
var size = 1;
var reg = "";
if (iscluster)
var features = feat.get('features');
size = features.length;
if (size > 1)
// draw circles

else
reg = features[0].get("Registre");

else
reg = feat.get("Registre");
if (size === 1)
style = styleSelectEquipement[reg];
return style;
;

getSelectStyle = function (selected)
var type = "";
var iscluster = isCluster(selected);
if (iscluster)
type = getTypeObjet(selected.get("features")[0]);
else
type = getTypeObjet(selected);
if (type === "Equipement")
return selectEquipementStyle(selected, iscluster);
return null;


selectInteraction = new ol.interaction.Select(
layers: [vectorLayerEquipement],
hitTolerance: 4,
style: getSelectStyle
);

map.getView().on('change:resolution', function (evt)
var view = evt.target;
if (view.getZoom() >= constante.NvZoomFeature)
clusterEquipementSource.setDistance(0);
else if (view.getZoom() < constante.NvZoomFeature)
clusterEquipementSource.setDistance(constante.DistanceCluster);

//Dispatch a select event with the clusters from clusterEquipementSource containing the previously selected clusters ?

, map);


Thanks for your time.










share|improve this question






















  • Clusters aren't real features, they are dynamically created by OpenLayers. When you change the zoom or distance OpenLayers will build a new set of clusters and destroy the previous ones, including any which were selected. It looks like the OL2 add-on works by creating a new "safeSelection" layer, containing single real unselected features styled to look like a selected cluster. That same could be done in later versions of OL, but there's no easy way to simply restyle.

    – Mike
    Nov 13 '18 at 11:48















0















I have a map with a vector layer and a cluster source. I choosed to display the clusters as circles with the number of features written inside. In case the cluster contains only one feature, it displays as a custom icon. If the zoom is too important, I set the distance between clusters at 0, to cancel the clusterization (I'm aware each feature is still considered as a cluster). I also have a select interaction with a custom fonction (Kadricarto.selectEquipementStyle) to change the style. The interaction works well and the clusters selected are displayed with the right style.



However, if I zoom in or out, the selected clusters are still displayed in the initial zoom of the selection, with their initial style.



Is there an easy way to dynamically re-style the selected clusters at each zoom level without dispatching a new select event ?



I stumbled upon this link (http://jorix.github.io/OL-FeaturePopups/examples/feature-popups.html) which produces the kind of behavior I want, but it's for OL2...
This is my code:



vectorEquipementSource = new ol.source.Vector();
clusterEquipementSource = new ol.source.Cluster(
source: vectorEquipementSource,
distance: constante.DistanceCluster
);
vectorLayerEquipement = new ol.layer.Vector(
source: clusterEquipementSource,
style: vectorEquipementStyle,
maxResolution: seuilVueCarto,
minResolution: minRes,
extent: maxextent,
visible: false
);

selectEquipementStyle = function (feat, iscluster)
var size = 1;
var reg = "";
if (iscluster)
var features = feat.get('features');
size = features.length;
if (size > 1)
// draw circles

else
reg = features[0].get("Registre");

else
reg = feat.get("Registre");
if (size === 1)
style = styleSelectEquipement[reg];
return style;
;

getSelectStyle = function (selected)
var type = "";
var iscluster = isCluster(selected);
if (iscluster)
type = getTypeObjet(selected.get("features")[0]);
else
type = getTypeObjet(selected);
if (type === "Equipement")
return selectEquipementStyle(selected, iscluster);
return null;


selectInteraction = new ol.interaction.Select(
layers: [vectorLayerEquipement],
hitTolerance: 4,
style: getSelectStyle
);

map.getView().on('change:resolution', function (evt)
var view = evt.target;
if (view.getZoom() >= constante.NvZoomFeature)
clusterEquipementSource.setDistance(0);
else if (view.getZoom() < constante.NvZoomFeature)
clusterEquipementSource.setDistance(constante.DistanceCluster);

//Dispatch a select event with the clusters from clusterEquipementSource containing the previously selected clusters ?

, map);


Thanks for your time.










share|improve this question






















  • Clusters aren't real features, they are dynamically created by OpenLayers. When you change the zoom or distance OpenLayers will build a new set of clusters and destroy the previous ones, including any which were selected. It looks like the OL2 add-on works by creating a new "safeSelection" layer, containing single real unselected features styled to look like a selected cluster. That same could be done in later versions of OL, but there's no easy way to simply restyle.

    – Mike
    Nov 13 '18 at 11:48













0












0








0








I have a map with a vector layer and a cluster source. I choosed to display the clusters as circles with the number of features written inside. In case the cluster contains only one feature, it displays as a custom icon. If the zoom is too important, I set the distance between clusters at 0, to cancel the clusterization (I'm aware each feature is still considered as a cluster). I also have a select interaction with a custom fonction (Kadricarto.selectEquipementStyle) to change the style. The interaction works well and the clusters selected are displayed with the right style.



However, if I zoom in or out, the selected clusters are still displayed in the initial zoom of the selection, with their initial style.



Is there an easy way to dynamically re-style the selected clusters at each zoom level without dispatching a new select event ?



I stumbled upon this link (http://jorix.github.io/OL-FeaturePopups/examples/feature-popups.html) which produces the kind of behavior I want, but it's for OL2...
This is my code:



vectorEquipementSource = new ol.source.Vector();
clusterEquipementSource = new ol.source.Cluster(
source: vectorEquipementSource,
distance: constante.DistanceCluster
);
vectorLayerEquipement = new ol.layer.Vector(
source: clusterEquipementSource,
style: vectorEquipementStyle,
maxResolution: seuilVueCarto,
minResolution: minRes,
extent: maxextent,
visible: false
);

selectEquipementStyle = function (feat, iscluster)
var size = 1;
var reg = "";
if (iscluster)
var features = feat.get('features');
size = features.length;
if (size > 1)
// draw circles

else
reg = features[0].get("Registre");

else
reg = feat.get("Registre");
if (size === 1)
style = styleSelectEquipement[reg];
return style;
;

getSelectStyle = function (selected)
var type = "";
var iscluster = isCluster(selected);
if (iscluster)
type = getTypeObjet(selected.get("features")[0]);
else
type = getTypeObjet(selected);
if (type === "Equipement")
return selectEquipementStyle(selected, iscluster);
return null;


selectInteraction = new ol.interaction.Select(
layers: [vectorLayerEquipement],
hitTolerance: 4,
style: getSelectStyle
);

map.getView().on('change:resolution', function (evt)
var view = evt.target;
if (view.getZoom() >= constante.NvZoomFeature)
clusterEquipementSource.setDistance(0);
else if (view.getZoom() < constante.NvZoomFeature)
clusterEquipementSource.setDistance(constante.DistanceCluster);

//Dispatch a select event with the clusters from clusterEquipementSource containing the previously selected clusters ?

, map);


Thanks for your time.










share|improve this question














I have a map with a vector layer and a cluster source. I choosed to display the clusters as circles with the number of features written inside. In case the cluster contains only one feature, it displays as a custom icon. If the zoom is too important, I set the distance between clusters at 0, to cancel the clusterization (I'm aware each feature is still considered as a cluster). I also have a select interaction with a custom fonction (Kadricarto.selectEquipementStyle) to change the style. The interaction works well and the clusters selected are displayed with the right style.



However, if I zoom in or out, the selected clusters are still displayed in the initial zoom of the selection, with their initial style.



Is there an easy way to dynamically re-style the selected clusters at each zoom level without dispatching a new select event ?



I stumbled upon this link (http://jorix.github.io/OL-FeaturePopups/examples/feature-popups.html) which produces the kind of behavior I want, but it's for OL2...
This is my code:



vectorEquipementSource = new ol.source.Vector();
clusterEquipementSource = new ol.source.Cluster(
source: vectorEquipementSource,
distance: constante.DistanceCluster
);
vectorLayerEquipement = new ol.layer.Vector(
source: clusterEquipementSource,
style: vectorEquipementStyle,
maxResolution: seuilVueCarto,
minResolution: minRes,
extent: maxextent,
visible: false
);

selectEquipementStyle = function (feat, iscluster)
var size = 1;
var reg = "";
if (iscluster)
var features = feat.get('features');
size = features.length;
if (size > 1)
// draw circles

else
reg = features[0].get("Registre");

else
reg = feat.get("Registre");
if (size === 1)
style = styleSelectEquipement[reg];
return style;
;

getSelectStyle = function (selected)
var type = "";
var iscluster = isCluster(selected);
if (iscluster)
type = getTypeObjet(selected.get("features")[0]);
else
type = getTypeObjet(selected);
if (type === "Equipement")
return selectEquipementStyle(selected, iscluster);
return null;


selectInteraction = new ol.interaction.Select(
layers: [vectorLayerEquipement],
hitTolerance: 4,
style: getSelectStyle
);

map.getView().on('change:resolution', function (evt)
var view = evt.target;
if (view.getZoom() >= constante.NvZoomFeature)
clusterEquipementSource.setDistance(0);
else if (view.getZoom() < constante.NvZoomFeature)
clusterEquipementSource.setDistance(constante.DistanceCluster);

//Dispatch a select event with the clusters from clusterEquipementSource containing the previously selected clusters ?

, map);


Thanks for your time.







javascript openlayers






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 10:11









AConcernedCitizenAConcernedCitizen

62




62












  • Clusters aren't real features, they are dynamically created by OpenLayers. When you change the zoom or distance OpenLayers will build a new set of clusters and destroy the previous ones, including any which were selected. It looks like the OL2 add-on works by creating a new "safeSelection" layer, containing single real unselected features styled to look like a selected cluster. That same could be done in later versions of OL, but there's no easy way to simply restyle.

    – Mike
    Nov 13 '18 at 11:48

















  • Clusters aren't real features, they are dynamically created by OpenLayers. When you change the zoom or distance OpenLayers will build a new set of clusters and destroy the previous ones, including any which were selected. It looks like the OL2 add-on works by creating a new "safeSelection" layer, containing single real unselected features styled to look like a selected cluster. That same could be done in later versions of OL, but there's no easy way to simply restyle.

    – Mike
    Nov 13 '18 at 11:48
















Clusters aren't real features, they are dynamically created by OpenLayers. When you change the zoom or distance OpenLayers will build a new set of clusters and destroy the previous ones, including any which were selected. It looks like the OL2 add-on works by creating a new "safeSelection" layer, containing single real unselected features styled to look like a selected cluster. That same could be done in later versions of OL, but there's no easy way to simply restyle.

– Mike
Nov 13 '18 at 11:48





Clusters aren't real features, they are dynamically created by OpenLayers. When you change the zoom or distance OpenLayers will build a new set of clusters and destroy the previous ones, including any which were selected. It looks like the OL2 add-on works by creating a new "safeSelection" layer, containing single real unselected features styled to look like a selected cluster. That same could be done in later versions of OL, but there's no easy way to simply restyle.

– Mike
Nov 13 '18 at 11:48












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53278593%2fhow-to-change-styles-of-selected-clusters-on-zoom-in-ol-4-6%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53278593%2fhow-to-change-styles-of-selected-clusters-on-zoom-in-ol-4-6%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus