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><altitudeMode>relativeToGround</altitudeMode><outerBoundaryIs><LinearRing><coordinates> 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 changefeatures
tofeatures[0]