【问题标题】:Reading and saving features from/to file - OpenLayers从/到文件读取和保存功能 - OpenLayers
【发布时间】:2012-05-23 18:44:36
【问题描述】:

我快疯了。我试图重现 OpenLayers 2.10 初学者指南中的一个示例,我试图在其中显示保存在 json 文件中的功能并在地图上添加功能,并将它们保存到文件中。

var map;    
function init(){
            map = new OpenLayers.Map('map');
            var options = {numZoomLevels: 3}
            var floorplan = new OpenLayers.Layer.Image(
            'Floorplan Map',
            'temp_photos/sample-floor-plan.jpg',
            new OpenLayers.Bounds(-180, -88.759, 180, 88.759),
            new OpenLayers.Size(580, 288),
            options
        );
            var roomPolygonLayer = new OpenLayers.Layer.Vector('Rooms', {
                protocol: new OpenLayers.Protocol.HTTP({
                    url: "myFloorPlanData.json", 
                    format: new OpenLayers.Format.GeoJSON({})}),
                strategies: [new OpenLayers.Strategy.Fixed(), new OpenLayers.Strategy.Save()]
            });

            map.addLayers([floorplan, roomPolygonLayer]);
            map.zoomToMaxExtent();
            map.addControl(new OpenLayers.Control.EditingToolbar(roomPolygonLayer));

            map.layers[1].onFeatureInsert = function(feature){
                alert("feature id: "+feature.id);
                alert("feature geometry: "+ feature.geometry);
            };
        }

到目前为止,我的地图已显示,我可以在地图上绘制矢量,但是它拒绝显示我在我的 json 文件中拥有的两个点,并且还保存了我绘制的新点:

{
"type": "FeatureCollection",
"features": [
    {"type":"Feature","properties":{}, "geometry":{"type":"Point", "coordinates":[5, 63]}},
    {"type":"Feature","properties":{}, "geometry":{"type":"Point", "coordinates":[-48, 27]}}
]

}

json 文件与我的 jsp 文件在同一个文件夹中,我正在服务器上运行我的项目

【问题讨论】:

  • 尝试用这个new OpenLayers.Format.GeoJSON()替换new OpenLayers.Format.GeoJSON({})
  • 我删除了编辑工具栏和点显示正确...我假设你不能使用相同的矢量图层来显示点和绘制点。但是我仍在努力保存我正在绘制的新功能,一旦我开始工作,我会在这里发布答案。我现在正在玩这个例子:openlayers.org/dev/examples/vector-formats.html

标签: serialization deserialization openlayers


【解决方案1】:

OpenLayers.Strategy.Save 不能直接修改 json 文件。它只能通过实现 WFS-T 协议的 Web 服务工作。

您可以安装支持 WFS-T 的软件,例如 Geoserver。然后您可以在您的 OpenLayers 应用程序中使用 OpenLayers.Strategy.Save。

另一种选择是创建将修改 json 文件的自定义 Web 服务。然后,您将创建某种保存按钮,单击该按钮将调用您的自定义 Web 服务。

【讨论】:

  • 我已经谢谢你了。我想在发布解决方案之前完成完整的示例:)
猜你喜欢
  • 2017-07-15
  • 2013-08-13
  • 1970-01-01
  • 2013-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多