【问题标题】:Adding multiple layers in OpenLayers 3在 OpenLayers 3 中添加多个图层
【发布时间】:2015-10-26 00:52:51
【问题描述】:

我通过依次分别从多个 GeoJSON 功能加载数组 multipointCoords 来填充 OpenLayer 3 功能。每次加载 GeoJSON 功能时,我都会重用以下函数:

   function makeLineString(multipointCoords) {

        var trackSource = new ol.source.Vector();

        var lineString = new ol.geom.LineString([ // create a linestring geometry from the GeoJSON data for the tracks
            ol.proj.fromLonLat(multipointCoords[0][0])
        ]);

        var trackFeature = new ol.Feature({ // create a feature with the linestring geometry
            geometry: lineString
        });

        trackSource.addFeature(trackFeature); // add the feature to the sourc

        var trackLayer = new ol.layer.Vector({ // create a layer with that source
            source: trackSource
        });
        map.addLayer(trackLayer); // add the layer to the map
};

我是将所有特征(无论它们来自哪个数据集)加载到同一层还是创建了多个“trackLayers”?有没有办法解决它们来自数据集的层和特征?

【问题讨论】:

  • 别忘了提供反馈?对吗?

标签: openlayers-3


【解决方案1】:

是的,您正在为makeLineString 的每次调用创建一个新源(具有单个功能)和一个新层。

您通常希望将给定数据集中的所有特征放入同一源(因此也放入同一层)。最常见的方法是使用代表远程数据集的urlformat 配置源。

将 GeoJSON 文件中的所有特征加载到同一源中并在 official examples: 的单个层中显示它们的示例

var vector = new ol.layer.Vector({
  source: new ol.source.Vector({
    url: 'data/geojson/countries.geojson',
    format: new ol.format.GeoJSON(),
    wrapX: false
  })
});

【讨论】:

  • 谢谢阿尔文。你能改写或解释你的最后一句话吗?我正试图围绕源和层的来龙去脉。几何形状和特征似乎更清晰:它们似乎一对一地映射。
  • 是的,那句话很奇怪。现在好点了吗?
  • 是的。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-11
  • 1970-01-01
  • 2022-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多