1

I'm trying to load a KML file into an Openlayers map. It seems to successfully run but I am not seeing anything show up on the map.

Vectors should show up in an area of New Mexico if it's working correctly.

Here is my code (here is a link to the KML file )

var map;

var baseMaps = [];
var sensors = [];

//create map and center over location
var init = function () {
    // create map

    map = new OpenLayers.Map({
        div: "map",
        theme: null,
        projection:new OpenLayers.Projection("EPSG: 4326"),
        controls: [
            new OpenLayers.Control.Attribution(),
            new OpenLayers.Control.TouchNavigation({
                dragPanOptions: {
                    enableKinetic: true
                }
            }),
            new OpenLayers.Control.Zoom()
        ],
        layers: [
            new OpenLayers.Layer.OSM()
        ]
    });

    map.setCenter(new OpenLayers.LonLat(-106.7653, 32.3197).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject()),9);
};

//show kmlFile
var showSensor = function(sensorVO)
{
    if(sensorVO.selected == true)
    {
        var kmllayer = new OpenLayers.Layer.Vector("KML", {
            strategies: [new OpenLayers.Strategy.Fixed()],
            protocol: new OpenLayers.Protocol.HTTP({
                url: sensorVO.url,
                format: new OpenLayers.Format.KML({
                    extractStyles: true, 
                    extractAttributes: true,
                    maxDepth: 2
                })
            })
        });

        map.addLayer(kmllayer);
        var select = new OpenLayers.Control.SelectFeature(kmllayer);
        map.addControl(select);
        select.activate();  

        map.setCenter(new OpenLayers.LonLat(-106.639261,32.649908).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject()),10);

        var obj = {"vo":sensorVO, "kmlLayer":kmllayer};
        sensors.push(obj);
    }

    else
    {

        for (var i = 0; i < sensors.length; i++) 
        {
            var vo = sensors[i].vo;
            if(vo.id == sensorVO.id)
            {
                map.removeLayer(vo.kmlLayer);
                sensors.splice(i, 1);
            }

        }

    }

}

init();

setTimeout(function(){
    var obj = {"id":1, "selected":true, "url":"kml-files/1858-Vegitation-Map.kml"};

    showSensor(obj);
}, 1000);
1

I couldn't access your KML file to see if it's storing points of lines, but that would be a starting check. KML can support the following geometries: Point, LineString, LinearRing, Polygon and MultiGeometry. Since a KML file is ASCII rather than binary you can open it in a text editor to make sure the coordinates are defined to the correct geometery. The coordinates should be clearly specified. Does the KML say that they are or ?

  • I tried zipping the file and sharing. see if that helps ge.tt/2EdNlIW/v/0 – mattwallace Jan 23 '13 at 21:46
  • I think you probably have your kml file uploaded correctly, I just can't retrieve due to a firewall from my end – user12711 Jan 24 '13 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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