1

The current way to add layer from KML file is:

    var urlToKml = someUrl;
    var vector = new ol.layer.Vector({
               source: new ol.source.Vector({
                        url: urlToKml,
                        format: new ol.format.KML()
                    }),
                    visible: true
                });

   map.addLayer(vector);

I'd like to load my source from a string

(ie: where urlToKml = "<kml xmlns='http://www.opengis.net/kml/2.2' xmlns:gx='http://www.google.com/kml/ext/2.2' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.opengis.net/kml/2.2 https://developers.google.com/kml/schema/kml22gx.xsd'><Placemark><description>hrt</description><Style><IconStyle><Icon><href>data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==</href><gx:w>5</gx:w><gx:h>5</gx:h></Icon><hotSpot x='2.5' y='2.5' xunits='pixels' yunits='pixels'/></IconStyle></Style><Point><coordinates>2.335711304736855,49.10454920376088</coordinates></Point></Placemark></kml>"; )

Is it possible and how?

1

As long as you have your kml as string, there is no need to use url parameter

Check the following snip:

var kmlstring = "<kml xmlns='http://www.opengis.net/kml/2.2' xmlns:gx='http://www.google.com/kml/ext/2.2' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.opengis.net/kml/2.2 https://developers.google.com/kml/schema/kml22gx.xsd'><Placemark><description>hrt</description><Style><IconStyle><Icon><href>data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==</href><gx:w>5</gx:w><gx:h>5</gx:h></Icon><hotSpot x='2.5' y='2.5' xunits='pixels' yunits='pixels'/></IconStyle></Style><Point><coordinates>2.335711304736855,49.10454920376088</coordinates></Point></Placemark></kml>";
//Your kml is projected in EPSG:4326
//and I assume your map is projected in 'EPSG:3857'
//Change them accordingly if different
var vectorSource = new ol.source.Vector({});
var features = new ol.format.KML().readFeatures(kmlstring ,{
                       dataProjection:'EPSG:4326',
                       featureProjection:'EPSG:3857'
                    });
vectorSource.addFeatures(features);
  • 1
    Just a small precision - I needed to add: var vector = new ol.layer.Vector({source: vectorSource}); map.addLayer(vector); But thank you so much! – F3L1X79 Nov 25 '16 at 11:04
  • 1
    glad to help amigo!!! – pavlos Nov 25 '16 at 11:13
  • Oh, and how did you know my KML projection is EPSG:4326? – F3L1X79 Nov 25 '16 at 11:37
  • 1
    I wasnt 100% sure but I just assumed from this ><Point><coordinates>2.335711304736855,49.10454920376088</coordinates> – pavlos Nov 25 '16 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.