Vaadin Map: How to enforce order of marker features?

Using Vaadin Maps 24.9.9 I’ve found that I can control which icons are “on top” by adding these MarkerFeatures to the map in a deterministic order, adding the topmost icons last.

However, I’ve found an odd case where if I zoom in so a feature is outside the frame, remove it, and then zoom out and re-add it, then it insists on being “on top” regardless of the order I add it in. (It’s a bit hard to explain - see the attached video.)

Here is the code I use to achieve this; basically on each checkbox change I remove all features from the map and the re-add them in a certain order, adding the pin at the middle (defaultFeature in my code) as the last step so it should always be on top.

// Maps each layer to a group of features.
final protected Map<String, List<Feature>> featureLayers = new LinkedHashMap<>();

// Maps each layer to a checkbox which controls its visibility.
final protected Map<String, Checkbox> featureLayersCheckboxes = new LinkedHashMap<>();

...
final Checkbox layerCheckbox = vaadinUtil.makeCheckbox(label);
layerCheckbox.addValueChangeListener(event -> {

	final FeatureLayer featureLayer = map.getFeatureLayer();
	featureLayer.removeAllFeatures();

	for (String featureKey : featureLayers.keySet()) {
		if (featureLayersCheckboxes.get(featureKey).getValue()) {
			featureLayers.get(featureKey).forEach(markerFeature -> featureLayer.addFeature(markerFeature));
		}
	}

	// Remove and re-add the default feature so that it is always on top
	featureLayer.addFeature(defaultFeature);

});

Hi, could you please create a ticket in GitHub · Where software is built ? That way it won’t get lost in Forum discussion.

I think using multiple feature layers should work. Apart from using the pre-configured feature layer, you can add as many as you want and define a stacking order for them:

FeatureLayer bottomLayer = new FeatureLayer();
bottomLayer.setzIndex(10);
map.addLayer(bottomLayer);

FeatureLayer middleLayer = new FeatureLayer();
middleLayer.setzIndex(20);
map.addLayer(middleLayer);

FeatureLayer topLayer = new FeatureLayer();
topLayer.setzIndex(30);
map.addLayer(topLayer);

This also seems to map to your data structure from what is visible in the example. If you want to hide all features in a specific layer you can just toggle its visibility with setVisible instead of removing features.

Note that the default feature layer has a z-index of 100, so if you want a custom layer to show up on top you need to adapt the z-index accordingly.

Thank you, Sascha. FeatureLayers fixes my issue; I wasn’t aware of that functionality, and my code above was a workaround for the fact that I thought it was missing.

Olli: The original behavior seems “not quite right” to me but it is no longer blocking me. Do you still want me to submit a ticket?

Up to you. If you feel like it’s worth further attention, then go for it.