Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a working map that retrieves KML from a url, but now I need to get the KML data from a local variable instead. Is there support for this, and if so what is the syntax. Searched the api and various sites but no luck finding anything about this. Thanks!


map = new OpenLayers.Map({
        div: "map",
        layers: [
            new OpenLayers.Layer.WMS(
                "WMS", "",
                {layers: "basic"}
            new OpenLayers.Layer.Vector("KML", {
                strategies: [new OpenLayers.Strategy.Fixed()],
                protocol: new OpenLayers.Protocol.HTTP({
                    url: "kml_large.kml",
                    format: new OpenLayers.Format.KML({
                        extractStyles: true, 
                        extractAttributes: true,
                        maxDepth: 2
        center: new OpenLayers.LonLat(-81, 28),
        zoom: 7
share|improve this question
up vote 6 down vote accepted

This will create OpenLayers.Feature.Vector Features from the KML string.

You might need to modify the projection info to fit your needs:

function GetFeaturesFromKMLString (strKML) {
    var format = new OpenLayers.Format.KML({
        'internalProjection': myMapObject.baseLayer.projection,
        'externalProjection': new OpenLayers.Projection("EPSG:4326")

Then you can do something like:

share|improve this answer
Ok, cool, so now I get what a Format object does, however, how would I integrate this with my current code? Sorry to ask for hand-holding but I find the API docs for OL to be really poor... (e.g. the OpenLayers.Layer.Vector constructor does not make it clear what an Options object should be composed of) Thanks again. See code posted to my original question. – bethesdaboys Nov 7 '11 at 19:21
Never mind that last question, I think I get it now. – bethesdaboys Nov 7 '11 at 19:32
sure? let me know... or post another question :) – CaptDragon Nov 7 '11 at 19:35
Needs the option extractStyles: true to pick up the styles – zod May 3 '13 at 19:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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