I have a problem with adding a vector layer from kml file. I am trying to make XMLHttpRequest and add only a placemark with specific id. Nothing is displaying on the map. Request is successful and variable data contains proper snippet of kml file. I'm pretty sure I'm making some noob mistake. Here is my code:
map = new OpenLayers.Map("mapdiv");
map.addLayer(new OpenLayers.Layer.OSM());
var idOfPlacemark = "test";
var req = new XMLHttpRequest();
req.open('GET', 'boundaries.kml', false);
req.send(null);
if(req.status == 200) dump(req.responseText);
var data = req.responseXML.querySelector("[id=" + idOfPlacemark + "]");
var format = new OpenLayers.Format.KML;
test_layer = new OpenLayers.Layer.Vector("test");
test_placemark = format.read(data.outerHTML);
test_layer.addFeatures(test_placemark);
map.addLayer(test_layer);
var layer_switcher= new OpenLayers.Control.LayerSwitcher({});
map.addControl(layer_switcher);
var lonLat = new OpenLayers.LonLat(19.274483, 50.203275)
.transform(
new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
map.getProjectionObject() // to Spherical Mercator Projection
);
var zoom=13;
map.setCenter (lonLat, zoom);
And here is my kml file:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0">
<Document>
<Placemark id="test">
<name>Name of placemark</name>
<description>This is descr</description>
<Polygon>
<extrude>1</extrude>
<altitudeMode>relativeToGround</altitudeMode>
<outerBoundaryIs>
<LinearRing>
<coordinates> 19.212462, 50.200544
19.21287, 50.200399
19.21346, 50.19998
19.213857, 50.199603
19.214146, 50.199548
19.214715, 50.199541
19.215766, 50.199761
19.216743, 50.199987
19.217311, 50.200083
19.218041, 50.200028
19.21876, 50.199871
19.219178, 50.19961
19.219446, 50.199376
19.219253, 50.199046
19.218331, 50.198318
19.217741, 50.197845
19.217333, 50.197508
19.217086, 50.196986
19.216421, 50.196279
19.215606, 50.196004
19.214737, 50.196024
19.213996, 50.196182
19.213331, 50.196423
19.212065, 50.197055
19.211089, 50.1977
19.210874, 50.198298
19.210896, 50.199005
19.211217, 50.199747
19.211647, 50.200228
19.212054, 50.200592
19.212301, 50.200598 </coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>