在 OpenLayers 5 中获取 KML 样式

Get KML style in OpenLayers 5

我在应用程序中有一些代码可以访问 KML 图层中所选要素的样式。它在 OpenLayers 3.1 中工作。我现在已经升级到 5.3.0,但它停止工作了。请参阅下面的相关行:

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));

行:

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

产生在开发者控制台中可见的错误:

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

我在文档或示例中找不到任何内容来说明如何正确访问给定地图项(不是整个 layer/source)的 KML 样式数据。有没有新的方法可以做到这一点,或者我错过了什么?

Could it have to do with this?: https://github.com/IGNF/geoportal-sdk/issues/2 Plugged into Google translate 它似乎在说不再在每个功能中存储样式属性,但它似乎没有说明它们在哪里存储...

KML 有效并正确显示在地图上。我似乎再也找不到访问样式数据的方法了。

在 OpenLayers 3 和 4 中,特征样式函数仅采用分辨率参数但在内部使用 this 因此函数或调用必须绑定到特征:

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

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

在 OpenLayers 5 中,特征样式函数类似于图层样式函数,需要将特征作为参数传递:

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