【问题标题】:openLayers addFeature to a vectorsource from xmlhttprequest in angularopenLayers addFeature 从 xmlhttprequest 角度的向量源
【发布时间】:2021-01-22 19:57:21
【问题描述】:

我从他们使用的 https://openlayers.org/en/latest/apidoc/module-ol_source_Vector-VectorSource.html 那里得到了启发

   vectorSource.addFeatures(vectorSource.getFormat().readFeatures(xhr.responseText));

但是在使用 typescript 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,或者我做错了?

【问题讨论】:

    标签: angular xmlhttprequest openlayers


    【解决方案1】:

    首先找到了使用 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');
                        }
                    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-18
      • 2021-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-18
      • 1970-01-01
      • 2015-10-07
      相关资源
      最近更新 更多