简体   繁体   中英

Get KML style in OpenLayers 5

I have some code in an application that access the style of a selected feature in a KML layer. It was working in OpenLayers 3.1. I have now upgraded to 5.3.0 and it stopped working. See the relevant lines below:

var featStyle = feature.getStyleFunction().call(feature, map.getView().getResolution());
var strokeWidth = featStyle[0].getStroke().getWidth();
var strokeColor = featStyle[0].getStroke().getColor();
var fillColor = featStyle[0].getFill().getColor();
var fillOpacity = (Math.round(fillColor[3] * 100));

The line:

var featStyle = feature.getStyleFunction().call(feature, map.getView().getResolution());

Produces an error visible in the developer console:

TypeError: o.getGeometry is not a function[Learn More] KML.js:943
    a KML.js:943
    myFunctionName file.php:5371
    onclick file.php:1

I can't find anything in the documentation or examples that shows how to properly access the KML style data for a given feature (not an entire layer/source). Is there a new way to do this or did I miss something?

Could it have to do with this?: https://github.com/IGNF/geoportal-sdk/issues/2 Plugged into Google translate it seems to say something about no longer storing style properties inside each feature but it does not seem to say where they are stored...

The KML is valid and displays on the map properly. I just can't seem to find a way to access the style data anymore.

In OpenLayers 3 and 4 a feature style function takes only a resolution argument but internally uses this so the function or call must be bound to the feature:

feature.getStyleFunction().bind(feature)(map.getView().getResolution());

or

feature.getStyleFunction().call(feature, map.getView().getResolution());

In OpenLayers 5 feature style function are similar to layer style functions and require the feature to be passed as an argument:

feature.getStyleFunction()(feature, map.getView().getResolution());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM