[英]Access geojson key-value pairs that are not geometry or properties?
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-77.155585,
40.056708,
0
],
[
-77.150315,
40.04536,
0
]
]
]
},
"id": 42001030101,
"Households": 1000,
"Income": 74597
},
我正在使用谷歌地圖 JS API。 我不能使用getProperty
function 因為數據沒有組合在一起的屬性。
如何訪問這些數據中的每一個?
這是我在意識到不能使用屬性 function 之前嘗試過的方法。
map.data.setStyle(
function(feature){
let income = feature.getProperty('Income');
let color = 'blue';
if (income > 10000){
color = 'red'
}
return {
fillColor: color,
//strokeColor: "green",
strokeWeight: 0.3,
};
}
);
根據geojson規范:
功能 object 有一個名為“properties”的成員。 The value of the properties member is an object (any JSON object or a JSON null value).
由於您的功能沒有properties
成員,您可以“修復”它,以便將任何既不是type
也不是geometry
的成員捆綁為properties
的成員。
這應該使用feature.getProperty()
啟用您現有的代碼。
const data = { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": { "type": "Polygon", "coordinates": [ [ [-77.155585, 40.056708, 0], [-77.150315, 40.04536, 0] ] ] }, "id": 42001030101, "Households": 1000, "Income": 74597 } ] } const repairGeoJsonProps = (fc) => { return { "type": "FeatureCollection", "features": fc.features.map(ftr => { const props = Object.entries(ftr).filter(k => ["type", "geometry"].indexOf(k[0]) < 0); return { "type": ftr.type, "geometry": ftr.geometry, "properties": Object.fromEntries(props) } }) } } console.log(repairGeoJsonProps(data));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.