【问题标题】:Features are tiny功能很小
【发布时间】:2021-06-10 15:09:04
【问题描述】:

我正在将我的地图从直接在源中获取 url 移动到我单独获取数据并手动添加它们的模型。像下面这样直接在源中获取 URL 效果很好。

var vectorSource = new ol.source.Vector({
    url: `${document.ApiRoot}/api/features/${this.Zoom}?coordinateList=${this.ViewportCoordinates.join(",")}`,
    format: new ol.format.GeoJSON()
});
await this.mainLayer.setSource(vectorSource);

所以现在我为我的图层创建了一个不再被替换的矢量源。我使用下面的代码来更新地图的新功能。

const features = await Api.GetFeatures(this.Zoom, this.ViewportCoordinates);

this.featureVectorSource.clear();
var geojsonFormat = new ol.format.GeoJSON();
const olFeatures = geojsonFormat.readFeatures(JSON.stringify(features));
this.featureVectorSource.addFeatures(olFeatures);

我使用浏览器调试工具检查了结果,一切看起来都正确。要素已正确解析并添加到地图中。如果我检查图层并获取其功能,那么它们就在那里。地图看起来几乎是空的,但我愤怒地四处滚动寻找东西,果然我找到了我的特征。

那个点是一个特征,我可以像以前一样选择它并从中读取数据。

我试过搞乱预测,但我并不完全清楚我在做什么。当我不需要时,似乎该功能正在以某种方式转换其坐标。我已经启用了使用鼠标指针在地图上显示的坐标,这似乎也很好。

我很茫然。任何帮助表示赞赏,谢谢。

【问题讨论】:

    标签: openlayers openlayers-6


    【解决方案1】:

    像往常一样,我在某件事上苦苦挣扎了几天,然后发布一个问题,然后在半小时内找到答案。

    对我来说,我必须使用选项阅读功能。您可以定义数据投影和特征投影。一旦我摆弄了这个,我就恢复了我的功能。我仍然不明白它是如何使用 url 而不是手动更新功能时“刚刚工作”的,但我终于通过了。

            static madoobry = {
                dataProjection: 'EPSG:4326',
                featureProjection: 'EPSG:3857'
            };
            private async updateMapWithNewFeatures(): Promise<void> {
                if (Number.isNaN(this.ViewportCoordinates[0])) return;
                debugger;
                const features = await Api.GetFeatures(this.Zoom, this.ViewportCoordinates);
    
                this.featureVectorSource.clear();
                var geojsonFormat = new ol.format.GeoJSON();
                const olFeatures = geojsonFormat.readFeatures(JSON.stringify(features), Globe.madoobry);
                this.featureVectorSource.addFeatures(olFeatures);
            }
    

    请原谅属性的愚蠢名称。关键变化是 madoobry 并在调用 readFeatures 时使用它。

    【讨论】:

      猜你喜欢
      • 2017-08-01
      • 1970-01-01
      • 2023-01-15
      • 2013-11-28
      • 2021-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多