Openlayers 3矢量源问题

时间:2015-07-18 02:30:12

标签: openlayers-3

在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

1 个答案:

答案 0 :(得分:1)

你的片段一切都很好。正如@kryger所说,AJAX是Asynchronous Javascript和XML。因此,注册一个监听器以了解何时将您的功能添加到源中,例如:

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

});