1

I am trying to load a KML file to Google Maps using OpenLayers 2, below is the code:

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

var sundials = new OpenLayers.Layer.Vector("KML", {
    projection: this.application.map.map.displayProjection,
    strategies: [new OpenLayers.Strategy.Fixed()],
    protocol: new OpenLayers.Protocol.HTTP({
        //url: "http://dev.openlayers.org/examples/kml/lines.kml",
        //url: this.application.CommonLocation + "scripts/Testing.kml",
        //url: 'data:application/vnd.google-earth.kml+xml;base64,' + this.tmpKml,
        url: 'data:application/vnd.google-earth.kml+xml;base64,' + "PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4NCjxrbWwgeG1sbnM9Imh0dHA6Ly93d3cub3Blbmdpcy5uZXQva21sLzIuMiI+DQogIDxEb2N1bWVudD4NCiAgICA8U3R5bGUgaWQ9ImJvdW5kYXJ5TGluZUNvbG9yMSI+DQogICAgICA8TGluZVN0eWxlPg0KICAgICAgICA8Y29sb3I+ZmYwMDAwMDA8L2NvbG9yPg0KICAgICAgICA8d2lkdGg+Mzwvd2lkdGg+DQogICAgICA8L0xpbmVTdHlsZT4NCiAgICAgIDxQb2x5U3R5bGU+DQogICAgICAgIDxmaWxsPmZhbHNlPC9maWxsPg0KICAgICAgPC9Qb2x5U3R5bGU+DQogICAgPC9TdHlsZT4NCiAgICA8Rm9sZGVyPg0KICAgICAgPG5hbWU+U295YmVhbiBZaWVsZDwvbmFtZT4NCiAgICAgIDxQbGFjZW1hcms+DQogICAgICAgIDxuYW1lPlNveWJlYW4gWWllbGQgKDEpPC9uYW1lPg0KICAgICAgICA8c3R5bGVVcmw+I2JvdW5kYXJ5TGluZUNvbG9yMTwvc3R5bGVVcmw+DQogICAgICAgIDxQb2x5Z29uPg0KICAgICAgICAgIDxvdXRlckJvdW5kYXJ5SXM+DQogICAgICAgICAgICA8TGluZWFyUmluZz4NCiAgICAgICAgICAgICAg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UxNTYwMzE3NjYzNzINCi05MS4zMzQ1Nzk5Mzg2NjkxLDMyLjUxNTMzMjM3NzE4NDgNCi05MS4zMzQ1MTA0MzkzMDU4LDMyLjUxMzMyOTEzNzM4MDMNCi05MS4zMzQ0OTY4MDU2NzQ1LDMyLjUxMjkxNDA2MTc0MzINCi05MS4zMzQyNjYzNzU5MTU0LDMyLjUwMzQ3NjY1MDA5Mjc8L2Nvb3JkaW5hdGVzPg0KICAgICAgICAgICAgPC9MaW5lYXJSaW5nPg0KICAgICAgICAgIDwvb3V0ZXJCb3VuZGFyeUlzPg0KICAgICAgICA8L1BvbHlnb24+DQogICAgICA8L1BsYWNlbWFyaz4NCiAgICA8L0ZvbGRlcj4NCiAgPC9Eb2N1bWVudD4NCjwva21sPg==",
        format: new OpenLayers.Format.KML({
            extractStyles: true,
            extractAttributes: true
        })
    })
});

However, Chrome throw the following error:

XMLHttpRequest cannot load data:application/vnd.google-earth.kml+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIi…AgICA8L1BsYWNlbWFyaz4NCiAgICA8L0ZvbGRlcj4NCiAgPC9Eb2N1bWVudD4NCjwva21sPg==. Request header field X-Requested-With is not allowed by Access-Control-Allow-Headers.

I have followed the step in:

https://stackoverflow.com/questions/12409600/error-request-header-field-content-type-is-not-allowed-by-access-control-allow

But still unable to solve it. Can someone please help?

1 Answer 1

0

Your encoded string is a KML document. You already have the features in your encoded string, right?

You can use the native Javascript atob() function to decode the string. Try it on the Javascript console.

Afterwards, use OpenLayers.Format.KML to parse the string to create a new vector layer.

There is already a SO question related with KML parsing in OpenLayers.

1
  • Thx jgorcha. I have followed your advice and managed to produce the xml string of the kml. I hae followed the link that you sent but stil unabel to show the layer on the map. I have posted this in a new question below: gis.stackexchange.com/questions/163614/…
    – jay
    Commented Sep 20, 2015 at 21:14

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.