OpenLayers 6.8:如何获得Geojson矢量坐标?



我有一个基本的地图与openlayers 6.8。我显示geojson中的标记。

geojson示例:

const geoMoscow = new ol.layer.Vector({
source: new ol.source.Vector({
url: './assets/moscow.geojson',
format: new ol.format.GeoJSON()
}),
visible: true,
title: 'geojson',
style: new ol.style.Style({
fill: fillStyle,
stroke: strokeStyle,
image: new ol.style.Circle({
fill: new ol.style.Fill({
color: [245, 49, 5, 1]
}),
radius: 10,
}),
})
})

我想得到这个geojson坐标以供进一步使用。我一直在阅读许多使用geojson.getSource(). getfeatures()的代码示例,该示例允许从每个特性访问getCoordinates()方法。

不幸的是,geoMoscow.getSource(). getfeatures()给出了一个空数组。

我显然错过了一些关于openlayers逻辑的东西。这似乎是完全可行的,因为我的geojson包含一个具有坐标的特征对象,但我无法访问它。

TLDR:如何从这个geojson Vector中访问坐标等信息。

必须等到源代码加载和解析完成

geoMoscow.getSource().on('featuresloadend', function(){
console.log(geoMoscow.getSource().getFeatures());
});

源在被添加到映射中之前不会加载。