[英]How to force OpenLayers to load source?
I have noticed that OpenLayers will not "load" source unless it is assigned to a layer which is assigned to map .我注意到OpenLayers不会“加载”源,除非它被分配给分配给map的层。
This code does console.log()
with number of features in a KML layer:此代码使用 KML 层中的许多功能执行
console.log()
:
const layer = new Heatmap({
source: new VectorSource({
url: file,
format: new KML({
extractStyles: false,
}),
}),
});
layer.getSource().on('change', function (evt: any) {
var source = evt.target;
if (source.getState() === 'ready') {
var numFeatures = source.getFeatures().length;
console.log('Count after change: ' + numFeatures);
}
});
this.map.addLayer(layer);
If I remove this line:如果我删除这一行:
this.map.addLayer(layer);
it doesn't output anything.它没有 output 任何东西。 I have a feeling that OpenLayers ignores it as it is not used.
我感觉OpenLayers忽略了它,因为它没有被使用。 Is there a way to force OpenLayers to load it?
有没有办法强制OpenLayers加载它?
(context - I would like to merge features coming from multiple sources into one thus I don't want to directly load them onto the map . I'd like them to load so I can get features and then merge these features into one array and then display this array on the map ) (上下文 - 我想将来自多个来源的功能合并为一个,因此我不想将它们直接加载到map上。我希望加载它们以便我可以获取功能,然后将这些功能合并到一个数组中然后在map上显示这个数组)
You can combine features from multiple files and formats by fetching the urls and using the format's readFeatures()
method您可以通过获取 url 并使用格式的
readFeatures()
方法来组合来自多个文件和格式的功能
const vectorSource = new VectorSource();
const layer = new Heatmap({
source: vectorSource,
});
function loadFromUrl (url, format) {
fetch(url).then(function (response) {
response.text().then(function (result) {
vectorSouce.addFeatures(
format.readFeatures(result, {
dataProjection: 'EPSG:4326',
featureProjection: map.getView().getProjection(),
})
}
});
}
loadFromUrl(kmlFile, new KML({extractStyles: false}));
loadFromUrl(geojsonFile, new GeoJSON());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.