[英]OpenLayers 6.8 : How to get Geojson Vector coordinates?
我有一個帶有 openlayers 6.8 的基本 map。 我顯示來自 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() 方法。
Unfort.netly geoMoscow.getSource().getFeatures() 給出了一個空數組。
我顯然遺漏了一些關於 openlayers 邏輯的東西。 這似乎完全可行,因為我的 geojson 包含一個特征 object,其中包含坐標,但我無法訪問它。
TLDR:如何從此 geojson Vector 訪問坐標等信息。
您必須等到源被加載和解析
geoMoscow.getSource().on('featuresloadend', function(){
console.log(geoMoscow.getSource().getFeatures());
});
在添加到 map 之前,源不會加載。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.