hasnat safder
hasnat safder

Reputation: 5

openlayers geojson read as wkt

Is there a way to read geojson in openlayers and convert to WKT format , my problem is that when i add more than one geojson as vector layer , they don't appear as one layer , kindly tell me how to display multiple geojson as part of a single vector layer , my code is

for (var i = 0; i < content.GeoJsonFiles.length; i++) {
    //color = content.Settings.BreakStyles[totalcolor].Color;
    //totalcolor++;
    var id = content.GeoJsonFiles[i];
    // alert(content.GeoJsonFiles[i]);
    var geojson_layer = new OpenLayers.Layer.Vector("GeoJSON", {
        strategies: [new OpenLayers.Strategy.Fixed()],
        protocol: new OpenLayers.Protocol.HTTP({
        url:'i.geojson' ,
        format: new OpenLayers.Format.GeoJSON({})
        }), renderers: ["Canvas", "SVG", "VML"]
    });
    map.addLayer(geojson_layer);
}

Upvotes: 0

Views: 928

Answers (1)

kryger
kryger

Reputation: 13181

  • 'i.geojson' will always evaluate to "i.geojson" string, no matter what the value of i is. You probably wanted content.GeoJsonFile[i].content, assuming content actually contains URLs, not raw GeoJSON data
  • You're creating a new vector layer and add it to the map in each loop iteration. What you're getting ("they don't appear as one layer") is exactly what you told the code to do.

High-level outline of what needs to be done instead (assuming .content contains raw GeoJSON, I suspect this is the case):

var geoJsonLayer = new OpenLayers.Layer.Vector("GeoJSON", {
    // layer options
});

for (var i = 0; i < content.GeoJsonFiles.length; i++) {
    var feature = content.GeoJsonFiles[i].content;
    geojsonLayer.addFeatures([feature]);
}

map.addLayer(geoJsonLayer);

You'd need to clarify what content.GeoJsonFiles actually contains and what WKT has to do with all this for a more precise answer.

Upvotes: 1

Related Questions