Search code examples
kmlopenlayers-3

What are the functions that can use with feature.getStyle() function in Openlayers 3?


I have used the following code to read the KML file. How to get the Styles which extracted from the kml file. Am i using the getStyle() function properly?

var parser = new ol.format.KML({extractStyles: true});  

var testfeat=parser.readFeatures(kmlAsString,{featureProjection:'EPSG:3857' });

for(i=0;i<testfeat.length;i++)
{
   console.log(testfeat[i].getStyle()); // What are the functions that use with getStyle()?
}

Solution

  • OpenLayers' ol.format.KML reads the style definitions within the KML file and stores them internally. When setting the style to a feature, the format object does it using a style function, i.e. ol.FeatureStyleFunction, see: http://openlayers.org/en/latest/apidoc/ol.html#.FeatureStyleFunction

    It does so here, using the feature setStyle method: https://github.com/openlayers/ol3/blob/v3.20.0/src/ol/format/kml.js#L1852

    I style function can't return properties like a normal ol.style.Style object can. It relies on the map resolution to calculate properties on the fly that can change.

    In summary: you can't access to the style definitions from those read out of a KML file using the ol.format.KML.