Wednesday 26 April 2017

openlayers 2 - Show KML from variable instead of URL


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 = ' Soybean Yield Soybean Yield (1) #boundaryLineColor1 -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 '
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.



Answer




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 = ' Soybean Yield Soybean Yield (1) #boundaryLineColor1 -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 '
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


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...