[GIS] How to add a KML vector from a string variable

kmlopenlayers

I have a variable that contains KML data how so I tried to do some research and come up with this code

var kmlString = result;

var features = new ol.format.KML().readFeatures(kmlString, {
    dataProjection: 'EPSG:4326'
    });

var KMLvectorSource = new ol.source.Vector({});

var KMLvector = new ol.layer.Vector({
    source: KMLvectorSource,
    visible: true
    });

KMLvectorSource.addFeature(features);
map.addLayer(KMLvector);

but I don't know why it doesn't work, I get error message: a.addEventListener is not a function

My KML string is:

<Polygon><extrude>0</extrude><tessellate>0</tessellate><alti‌​tudeMode>relativeToG‌​round</altitudeMode>‌​<outerBoundaryIs><Li‌​nearRing><coordinate‌​s> list of coordinates </coordinates></LinearRing></outerBoundaryIs></Polygon>

Best Answer

because of KMLvectorSource.addFeature(features);

addFeature for single feature but features is an array though its length is 1.
So, use addFeatures instead or change features to features[0]

Related Question