1

I am trying to add a layer from KML variable instead of URL due to the current circumstances that force me to. I am using OpenLayers 2.13.1, Chrome, Visual Studio and .NET.

I have read the following link and followed them but still unable to show the result.

In OpenLayers can I replace a kml's URL for OpenLayers.Protocol.HTTP with the actual text of the kml?

How to add KML data but from variable - not from url?

OpenLayers 2.12 + KML from String + Change markers'icons

Below is my code:

var wms = new OpenLayers.Layer.WMS(
          "OpenLayers WMS",
          "http://vmap0.tiles.osgeo.org/wms/vmap0",
          { layers: 'basic' }
      );

var kml_string = '<?xml version="1.0" encoding="utf-8"?> <kml xmlns="http://www.opengis.net/kml/2.2">   <Document>     <Style id="boundaryLineColor1">       <LineStyle>         <color>ff000000</color>         <width>3</width>       </LineStyle>       <PolyStyle>         <fill>false</fill>       </PolyStyle>     </Style>     <Folder>       <name>Soybean Yield</name>       <Placemark>         <name>Soybean Yield (1)</name>         <styleUrl>#boundaryLineColor1</styleUrl>         <Polygon>           <outerBoundaryIs>             <LinearRing>               <coordinates>-91.3342663759154,32.5034766500927 -91.3342667084831,32.5034586155548 -91.3342986325171,32.5028817921232 -91.3347528643046,32.5013364566058 -91.3353490833926,32.5013263087953 -91.3371799720313,32.501314445573 -91.338116698321,32.5013087851122 -91.3386487408102,32.501315812754 -91.3386663742212,32.5015144742388 -91.3387262951512,32.5028862267216 -91.3387406121766,32.5032652339614 -91.3387805784262,32.5045645667511 -91.3389131380096,32.5089317435004 -91.3389218164796,32.5096173380048 -91.3389859320576,32.5153906443518 -91.3389832778257,32.5155349207259 -91.3388110064899,32.5156408789761 -91.337874131254,32.5156465397406 -91.3359365262234,32.5156569954422 -91.334957081528,32.5156620711925 -91.3346590909512,32.515658125708 -91.3345962338078,32.5156031766372 -91.3345799386691,32.5153323771848 -91.3345104393058,32.5133291373803 -91.3344968056745,32.5129140617432 -91.3342663759154,32.5034766500927</coordinates>             </LinearRing>           </outerBoundaryIs>         </Polygon>       </Placemark>     </Folder>   </Document> </kml>'
var kmlParser = new OpenLayers.Format.KML({
    extractStyles: true,
    extractAttributes: true
}); // Initialize with options if necessary
var feature_list = kmlParser.read(kml_string);
var sundialsTest = new OpenLayers.Layer.Vector("Test");
sundialsTest.addFeatures(feature_list);
this.application.map.map.addLayers([wms, sundialsTest]);

Can someone help?

I am pretty new to OpenLayers.

2

I think there is no problem with your code. I did the same with in a simple html file and it worked. The only thing I had to change was the reference to the map.

In plain html + javascript you would reference the map simple by using the map variable.

map.addLayer(sundialsTest);

or

map.addLayers([sundialsTest]);

Make sure that this.application.map.mapis a reference to your OpenLayers map.

console.log(this.application.map.map);

I added a screenshot with the result. To make it, I changed the base layer to Bing, and I also reproject the string.

The entire code is:

    var map = null;
    var apiKey = "(...)"; // your Bing API key
    function init() {
        map = new OpenLayers.Map("map");
        map.addControl(new OpenLayers.Control.LayerSwitcher());
        var road = new OpenLayers.Layer.Bing({
            name: "Road",
            key: apiKey,
            type: "Road"
        });
        map.addLayers([road]);
        var kml_string = '<?xml version="1.0" encoding="utf-8"?> <kml xmlns="http://www.opengis.net/kml/2.2">   <Document>     <Style id="boundaryLineColor1">       <LineStyle>         <color>ff000000</color>         <width>3</width>       </LineStyle>       <PolyStyle>         <fill>false</fill>       </PolyStyle>     </Style>     <Folder>       <name>Soybean Yield</name>       <Placemark>         <name>Soybean Yield (1)</name>         <styleUrl>#boundaryLineColor1</styleUrl>         <Polygon>           <outerBoundaryIs>             <LinearRing>               <coordinates>-91.3342663759154,32.5034766500927 -91.3342667084831,32.5034586155548 -91.3342986325171,32.5028817921232 -91.3347528643046,32.5013364566058 -91.3353490833926,32.5013263087953 -91.3371799720313,32.501314445573 -91.338116698321,32.5013087851122 -91.3386487408102,32.501315812754 -91.3386663742212,32.5015144742388 -91.3387262951512,32.5028862267216 -91.3387406121766,32.5032652339614 -91.3387805784262,32.5045645667511 -91.3389131380096,32.5089317435004 -91.3389218164796,32.5096173380048 -91.3389859320576,32.5153906443518 -91.3389832778257,32.5155349207259 -91.3388110064899,32.5156408789761 -91.337874131254,32.5156465397406 -91.3359365262234,32.5156569954422 -91.334957081528,32.5156620711925 -91.3346590909512,32.515658125708 -91.3345962338078,32.5156031766372 -91.3345799386691,32.5153323771848 -91.3345104393058,32.5133291373803 -91.3344968056745,32.5129140617432 -91.3342663759154,32.5034766500927</coordinates>             </LinearRing>           </outerBoundaryIs>         </Polygon>       </Placemark>     </Folder>   </Document> </kml>'
        var kmlParser = new OpenLayers.Format.KML({
            extractStyles: true,
            extractAttributes: true,
            internalProjection: new OpenLayers.Projection("EPSG:3857"),
            externalProjection: new OpenLayers.Projection("EPSG:4326")
        });
        var feature_list = kmlParser.read(kml_string);
        var sundialsTest = new OpenLayers.Layer.Vector("Test");
        sundialsTest.addFeatures(feature_list);
        map.addLayer(sundialsTest);
        map.zoomToExtent(sundialsTest.getDataExtent());
    }

enter image description here

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.