简体   繁体   中英

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);
}
  • '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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM