在 OpenLayers 3 中添加多层

Adding multiple layers in OpenLayers 3

我通过依次分别从多个 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"?有没有办法解决它们来自哪个数据集的层和特征?

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

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

将 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
  })
});