Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
121 views
in Technique[技术] by (71.8m points)

openLayers addFeature to a vectorsource from xmlhttprequest in angular

I took insperation from https://openlayers.org/en/latest/apidoc/module-ol_source_Vector-VectorSource.html where they use

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

but in angular using typescript addFeatures accepts Feature[] while vectorSource.getFormat().readFeatures(xhr.responseText) gives FeatureLike[].

Argument of type 'FeatureLike[] | undefined' is not assignable to parameter of type 'Feature[]'. Type 'undefined' is not assignable to type 'Feature[]'.ts(2345)

heres the section which errors:

     xhr.onload = () => {
                    if (xhr.status === 200) {
                        source.addFeatures(
                            source
                                .getFormat()
                                ?.readFeatures(xhr.responseText)
                        );
                    } else {
                        console.log('error');
                    }
                };

is there a way to transform/cast FeatureLike to ordinary Feature, or maybe I'm doing this wrong?

question from:https://stackoverflow.com/questions/65844343/openlayers-addfeature-to-a-vectorsource-from-xmlhttprequest-in-angular

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

found a solution to transform the data by using GeoJSON first (ref: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');
                    }
                };

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...