question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feature.setStyle is not a function

See original GitHub issue

The feature object retrieved by the map.forEachFeatureAtPixel seems not having the setStyle and get method

map.forEachFeatureAtPixel(e.pixel, function (feature) {
    boundarySelected = feature;
    feature.setStyle(highlightStyle);
    return true;
});
if (boundarySelected) {
    status.innerHTML = boundarySelected.get('name');
} else {
    status.innerHTML = ' ';
}

The setStyle method and get(‘name’) method should exist like shown in the Hover Example and in the Feature documentation

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
MoonEcommented, Aug 25, 2021

The vector tiles are only rendered once and then cached, so changing the style of the feature afterwards will not work, plus you have the problem that the geometry may be split between multiple tiles and setting the style on just one of those features will give unexpected results.

When you look in the example in my previous comment you will find there is a separate layer created for highlighting the active geometry that uses the same source but renders with renderMode: 'vector', so no tile caching takes place. Using a style function and identifying the features by an id value will allow you to consistently style the entire geometry.

1reaction
MoonEcommented, Aug 25, 2021

Have a look at this example which is using a MVT source: https://openlayers.org/en/latest/examples/vector-tile-selection.html As written above the features of your tile source are not ol/Feature but ol/render/Feature which is a different class optimized for just rendering. They are not meant to be changed and there are a lot of functions not available compared to the normal ol/Feature objects.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot set a style for clicked features in vector layer
Solution 1: setStyle(null)​​ OpenLayers will use the feature's style if it has one, and otherwise use the layer's style. So re-setting the ...
Read more >
Openlayers - Unable to set the style of features
I have both tried feature.style = selected_polygon_style; and feature.setStyle(selected_polygon_style); . The first just doesn't change the ...
Read more >
feature is not passed to style function when set via .setStyle()
setStyle (stylefunction) , it doesn't work. The problem is that the feature is not passed on to the style function, only the resolution....
Read more >
OpenLayers v7.2.2 API - Class: Feature
setStyle (style) ... Set the style for the feature to override the layer style. This can be a single style object, an array...
Read more >
Data Layer | Maps JavaScript API - Google Developers
The setStyle() method takes either a StyleOptions object literal, or a function that computes the style for each feature.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found