【问题标题】:Openlayers 3 vector source issueOpenlayers 3 矢量源问题
【发布时间】:2015-10-07 20:18:12
【问题描述】:

在 ajax 回调中,我拥有预期的所有功能,但在外部没有。

我错过了什么?

var geojsonSource = new ol.source.Vector();
$.ajax('assets/data/data.geojson').then(function(response) {
    var geojsonFormat = new ol.format.GeoJSON();
    var features = geojsonFormat.readFeatures(response, {featureProjection: 'EPSG:4326'});

    geojsonSource.addFeatures(features);
    console.log(geojsonSource.getFeatures()); // this work
});
console.log(geojsonSource.getFeatures()); // this doesn't work

【问题讨论】:

  • 这不是OpenLayers(或向量源)的问题,这是因为JavaScript是异步的,最后一行被执行之前 AJAX处理函数被执行(它只是当远程数据完全加载时调用)。

标签: openlayers-3


【解决方案1】:

您的 sn-p 一切正常。正如@kryger 所说,AJAX 是异步 Javascript 和 XML。因此,注册一个侦听器以了解您的功能何时添加到源中,例如:

geojsonSource.on('addfeature', function(event){
    console.log(geojsonSource.getFeatures());

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多