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

Posted

技术标签:

【中文标题】如何从 openlayers 读取外部 GeoJSON 文件?【英文标题】:How to read external GeoJSON file from openlayers? 【发布时间】:2012-05-09 06:28:30 【问题描述】:

我必须通过 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】:
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。

【讨论】:

您好,请检查此链接以了解未正确加载的 geoJSON。 ***.com/questions/21448612/…

以上是关于如何从 openlayers 读取外部 GeoJSON 文件?的主要内容,如果未能解决你的问题,请参考以下文章

如何在openlayers中测量从圆心到边缘的距离

如何从PostGIS坐标中心openlayers地图?

openlayers 3读取加载geojson格式数据

如何从外部 IDP 读取/导入角色到 Keycloak

如何计算从外部服务读取 gcp Pub/Sub 消息的成本

如何从 Spark 结构化流中的 Cassandra 等外部存储读取 Kafka 和查询?