【发布时间】: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