Reputation: 950
I am populating OpenLayer 3 features by loading the array multipointCoords from multiple GeoJSON features separately in turn. I reuse the following function each time I load a GeoJSON feature:
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
};
Am I loading all of the features, regardless of which data set they came from, onto the same layer or are multiple "trackLayers" being created? Is there a way to address the layers and features by which data set they came from?
Upvotes: 0
Views: 4165
Reputation: 3142
Yes, you are creating a new source (with a single feature) and a new layer for each invocation of makeLineString
.
You generally want to put all features from a given dataset into the same source (and consequently the same layer). The most common way to do so is to configure a source with an url
and a format
representing the remote data set.
An example of loading all features from a GeoJSON file into the same source and displaying them in a single layer from the 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
})
});
Upvotes: 1