"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
},
I am using the google maps JS API. I cannot use the getProperty
function because the data doesn't have the properties grouped together.
How can I access each of these pieces of data?
Here is what I tried before I realized that you can't use the properties 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,
};
}
);
According to the geojson spec :
A Feature object has a member with the name "properties". The value of the properties member is an object (any JSON object or a JSON null value).
As your feature doesn't have a properties
member, you can 'repair' it so that any member that is neither type
nor geometry
is bundled as a member of properties
.
This should enable your existing code using 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));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.