Tuesday 22 January 2019

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


I had an OpenLayers map that reads in and displayed a kml like this:


var vector_format = new OpenLayers.Format.KML({extractAttributes: true});

var vector_protocol = new OpenLayers.Protocol.HTTP({

url: 'http://www.geos.ed.ac.uk/~s0825955/thomlines.kml',
format: vector_format
});

var vector_strategies = [new OpenLayers.Strategy.Fixed()];
vector_Layer = new OpenLayers.Layer.Vector('More Advanced Vector Layer',{
protocol: vector_protocol,
strategies: vector_strategies
});


I am producing the with a perl file and have the kml as a variable. Can I replace the url ({ url: 'http://www.geos.ed.ac.uk/~s0825955/thomlines.kml',) with the actual kml text as stored in my scalar variable $newkml ? and if so how?



Answer



I've never used scalar before, but if you can store the kml text as a javascript string, then you can just use an OpenLayers.Format.KML object to read geometries from the kml text.


Some thing like this:


var kml_string = "..."; // you kml text stored as a javascript string
var kmlParser = new OpenLayers.Format.KML(); // Initialize with options if necessary
var feature_list = kmlParser.read(kml_string);

The feature_list is the vector features from kml text.


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...