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?

1 Answer 1


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.

  • have you had anyproblems with the first line of KML having characters that javascript doesn't like? one of these: <?xml version="1.0" encoding="utf-8" ?> Do I need to add some backslackes? Jul 5, 2013 at 16:32
  • yes, backslash is one option. Another option is to use single quotation to quote you whole kml text.
    – mfdev
    Jul 5, 2013 at 16:41
  • have you had any issues with feature list being undefined when you call its methods? Jul 6, 2013 at 11:38
  • maybe i need to incorporate my vector_stretegies? When I was getting the kml by url i used: var vector_strategies = [new OpenLayers.Strategy.Fixed()]; Jul 6, 2013 at 11:45
  • 1
    I've just checked your kml file, and the file contains both single and double quotes inside. So the single quotes option will not work. I think there should be some way that you can assign your scalar variable ($newkml) to a javascript variable, maybe that's a better option.
    – mfdev
    Jul 6, 2013 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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