[英]How to force OpenLayers to load source?
我注意到OpenLayers不会“加载”源,除非它被分配给分配给map的层。
此代码使用 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);
如果我删除这一行:
this.map.addLayer(layer);
它没有 output 任何东西。 我感觉OpenLayers忽略了它,因为它没有被使用。 有没有办法强制OpenLayers加载它?
(上下文 - 我想将来自多个来源的功能合并为一个,因此我不想将它们直接加载到map上。我希望加载它们以便我可以获取功能,然后将这些功能合并到一个数组中然后在map上显示这个数组)
您可以通过获取 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.