正在加载源向量(功能不会显示?) [英] Loading source vector (Features won't show?)

查看:67
本文介绍了正在加载源向量(功能不会显示?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a function that loads a vector source from a local geojson file.

问题是,我需要使它对于其中的一层是远程的,但是尽管正确加载了源并且console.logs确实显示了它们的确获取了它们,但是这些功能却从未显示过……如果我将发生奇怪的事情省略以下行:"this.layerSwitcherGroup.getLayers().push(this.pointsLayer);"从代码.注释该行后,加载程序将永远不会运行(内部不会显示console.logs).

Problem is, I need it to be remote for one of the layers, yet the features never show despite loading the source properly and the console.logs showing me it did indeed fetch them...plus a weird thing happens if I omit the line: "this.layerSwitcherGroup.getLayers().push(this.pointsLayer);" from the code. When that line is commented, the loader never runs (no console.logs appear from inside it).

注意:我只是临时编辑crs,直到服务器中的文件的crs更新为非旧文件为止.通过下载本地文件并编辑crs部分,使用本地功能从服务器测试geojson文件时,我执行了相同的操作.本地功能有效,但远程功能无效.

Note: I am editing the crs only temporarily until the file in the server has the crs updated to a non-legacy one. I did the same when I tested the geojson file from the server with local function by downloading it and editing the crs part. Local function worked, but remote doesn't.

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;
},

上面列出的每个函数都可以在本地模式下使用,所以我不太确定远程加载程序在做什么错...

Every function that is listed above works with local mode, so I'm not quite sure what am I doing wrong with the remote loader...

推荐答案

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

So all I was missing was adding {featureProjection: 'EPSG:3857'} to readFeatures so that the features would be projected in the map view correctly... (since that is the map projection)

通过替换

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

使用

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

通过找到了它,https://stackoverflow.com/a/32455939/2340999 中显示了这些功能现在合适的位置!

Found it through https://stackoverflow.com/a/32455939/2340999 and the features are showing in the proper positions now!

感谢您的所有建议!

这篇关于正在加载源向量(功能不会显示?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆