正在加载源矢量(功能不会显示?)

Loading source vector (Features won't show?)

本文关键字:显示 功能 加载      更新时间:2023-09-26

我有一个从本地geojson文件加载矢量源的函数。

问题是,我需要它对其中一个层是远程的,但尽管正确加载了源代码,并且console.log显示它确实获取了这些功能,但这些功能从未显示。。。另外,如果我在代码中省略了"this.layerSwitcherGroup.getLayers().push(this.pointsLayer);"这一行,就会发生一件奇怪的事情。当对该行进行注释时,加载程序将永远不会运行(其中不会显示console.log)。

注意:我只是临时编辑crs,直到服务器中的文件将crs更新为非旧版本。当我从带有本地功能的服务器上测试geojson文件时,我也做了同样的事情,下载并编辑crs部分。本地功能有效,但远程功能无效。

addPoints: function() {
    this.addPointInteraction();
    this.pointsLayer = new ol.layer.Vector({
        source: new ol.source.Vector({
            /**
             * The function is responsible for loading the features and adding them to the source.
             * ol.source.Vector sources use a function of this type to load features.
             * @param extent - the area to be loaded
             * @param resolution - the resolution (map units per pixel)
             * @param projection - ol.proj.Projection for the projection as arguments
             *
             *  this (keyword): within the function is bound to the ol.source.Vector it's called from.
             */
            loader: function(extent, resolution, projection) {
                console.log('vector Loader...');
                var url = //can't show the url here;
                $.ajax({
                    url: url,
                    context: this,
                    success: function(json) {
                        console.log('Data: ', json);
                        json.data.crs = {
                            "type": "name",
                            "properties": {
                                "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
                            }
                        };
                        console.log('changed CRS: ', json);
                        var features = new ol.format.GeoJSON().readFeatures(json.data);
                        console.log('this inside loader: ', this);
                        this.addFeatures(features);
                    }
                });
            }
        }),
        style: this.defaultPointStyleFunction
    });
    this.layerSwitcherGroup.getLayers().push(this.pointsLayer);
    this.pointsLayer.getSource().once("change", function(evt) {
        console.log('pointsLayer once');
        //console.log('pointsLayer changed: ', this.pointsLayer);
        //console.log('pointsLayer source: ', this.pointsLayer.getSource());
        console.log('pointsLayer features: ', this.pointsLayer.getSource().getFeatures());
        //console.log('current layerSwitcherGroup layers: ', this.layerSwitcherGroup.getLayers());
        this.hidePoints();
        this.onSetSelection(1);
    }, this);
    this.currPointId = null;
},

上面列出的每个函数都使用本地模式,所以我不太确定远程加载程序出了什么问题。。。

所以我所缺少的只是将{featureProjection:'EPSG:3857'}添加到readFeatures中,以便在地图视图中正确投影这些特征。。。(因为那是地图投影)

通过更换修复

var features = new ol.format.GeoJSON().readFeatures(json.data);

var features = format.readFeatures(json.data, {featureProjection: 'EPSG:3857'});

找到了https://stackoverflow.com/a/32455939/2340999功能现在显示在正确的位置!

谢谢你的建议!

更改此

var features = new ol.format.GeoJSON().readFeatures(json.data);

到这个

var features = (new ol.format.GeoJSON()).readFeatures(json.data);

我使用此处提供的示例完成了此操作:http://openlayersbook.github.io/ch11-creating-web-map-apps/example-03.html

不确定这是否有帮助。