如何解决openLayers 将特征从 xmlhttprequest 以角度添加到矢量源
我从他们使用的 https://openlayers.org/en/latest/apidoc/module-ol_source_Vector-VectorSource.html 处进行了测试
vectorSource.addFeatures(vectorSource.getFormat().readFeatures(xhr.responseText));
但在角度使用打字稿 addFeatures 接受 Feature[] 而 vectorSource.getFormat().readFeatures(xhr.responseText) 给出 FeatureLike[]。
'FeatureLike[] 类型的参数 | undefined' 不能分配给类型为 'Feature[]' 的参数。 类型 'undefined' 不可分配给类型 'Feature[]'.ts(2345)
这是出错的部分:
xhr.onload = () => {
if (xhr.status === 200) {
source.addFeatures(
source
.getFormat()
?.readFeatures(xhr.responseText)
);
} else {
console.log('error');
}
};
有没有办法将 FeatureLike 转换/转换为普通的 Feature,或者我做错了?
解决方法
首先找到了使用 GeoJSON 转换数据的解决方案(参考:How to reference another answer?)
xhr.onload = () => {
if (xhr.status === 200) {
let features = new GeoJSON({
featureProjection: 'EPSG:32633',}).readFeatures(xhr.responseText);
source.addFeatures(features);
} else {
console.log('error');
}
};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。