如何从openlayers读取外部GeoJSON文件?

时间:2012-04-29 00:29:00

标签: map openlayers geo geojson

我必须通过OpenLayers绘制一些行。线要素编码为GeoJSON格式。我的代码适用于硬编码的GeoJSON功能。但是,如果我将此功能放在单独的文件中并尝试加载它。它只是不起作用。我不知道加载外部GeoJSON文件有什么问题。我已经给了这两个代码。

代码1:

// This code is ok with hard coded GeoJSON features
map.addControl(new OpenLayers.Control.LayerSwitcher());

            vectorLayer = new OpenLayers.Layer.Vector("Lines");

            var myGeoJSON = { "type": "FeatureCollection",
                "features": 
                [

                    { "type": "Feature", "properties": { "LENGTH": 756.304000}, "geometry": { "type": "LineString", "coordinates": [ [ 18.105018, 59.231027 ], [ 18.104176, 59.230737 ], [ 18.103928, 59.230415 ], [ 18.103650, 59.230336 ], [ 18.103028, 59.230463 ], [ 18.102491, 59.230418 ], [ 18.101976, 59.230237 ], [ 18.100893, 59.230110 ], [ 18.100117, 59.230016 ], [ 18.097715, 59.230262 ], [ 18.096907, 59.230376 ], [ 18.096637, 59.230405 ], [ 18.096578, 59.230428 ], [ 18.096429, 59.230450 ], [ 18.096336, 59.230479 ], [ 18.096108, 59.230534 ], [ 18.095971, 59.230600 ], [ 18.095925, 59.230633 ], [ 18.095891, 59.230665 ], [ 18.094000, 59.231676 ], [ 18.093864, 59.231720 ] ] } }
                    ,                   
            { "type": "Feature", "properties": { "LENGTH": 1462.390000}, "geometry": { "type": "LineString", "coordinates": [ [ 17.877073, 59.461653 ], [ 17.877116, 59.461598 ], [ 17.876936, 59.461507 ], [ 17.876936, 59.461323 ], [ 17.876773, 59.461098 ], [ 17.876430, 59.460885 ], [ 17.876413, 59.460553 ], [ 17.876576, 59.460280 ], [ 17.876575, 59.460078 ], [ 17.876762, 59.460060 ], [ 17.877371, 59.460042 ], [ 17.877808, 59.460046 ], [ 17.878641, 59.460046 ], [ 17.879010, 59.460078 ], [ 17.879337, 59.460044 ], [ 17.879526, 59.459878 ], [ 17.879749, 59.459563 ], [ 17.880058, 59.459538 ], [ 17.880435, 59.459503 ], [ 17.887550, 59.453608 ], [ 17.887696, 59.453430 ], [ 17.887971, 59.453150 ], [ 17.888221, 59.452843 ], [ 17.888246, 59.452721 ], [ 17.888435, 59.452609 ], [ 17.888470, 59.452568 ], [ 17.888517, 59.452410 ] ] } }

                ]
            };

            var geojson_format = new OpenLayers.Format.GeoJSON({
                'internalProjection': map.baseLayer.projection,
                'externalProjection': new OpenLayers.Projection("EPSG:4326")
            });


            map.addLayer(vectorLayer);

            vectorLayer.addFeatures(geojson_format.read(myGeoJSON));

            map.setCenter(
                new OpenLayers.LonLat(18.068611, 59.329444).transform(
                    new OpenLayers.Projection("EPSG:4326"),
                    map.getProjectionObject()
                ), 10
            );

代码2 :此代码显示无法加载功能的错误

//This code does not work because it can not load the external GeoJSON file

map.addControl(new OpenLayers.Control.LayerSwitcher());

vectorLayer = new OpenLayers.Layer.Vector("Lines");

var myGeoJSON = new OpenLayers.Layer.Vector("Lines", {
    strategies: [new OpenLayers.Strategy.Fixed()],                
    protocol: new OpenLayers.Protocol.HTTP({
           url: "ml/lines.json"

           })
     });

var geojson_format = new OpenLayers.Format.GeoJSON({
    'internalProjection': map.baseLayer.projection,
    'externalProjection': new OpenLayers.Projection("EPSG:4326")
    });

map.addLayer(vectorLayer);

vectorLayer.addFeatures(geojson_format.read(myGeoJSON));

map.setCenter(
     new OpenLayers.LonLat(18.068611, 59.329444).transform(
         new OpenLayers.Projection("EPSG:4326"),
             map.getProjectionObject()
                ), 10
            );

提前致谢

1 个答案:

答案 0 :(得分:29)

geojson_layer = new OpenLayers.Layer.Vector("GeoJSON", {
            strategies: [new OpenLayers.Strategy.Fixed()],
            protocol: new OpenLayers.Protocol.HTTP({
                url: "ml/lines.json",
                format: new OpenLayers.Format.GeoJSON()
            })
        });

查看我的小example