Reputation: 521
So, I'm starting to build a simple OpenLayers 4.6 application and I'm trying to make a simple map with some point features. I was trying to modify the GeoJSON example on the OpenLayers site. The issue that I'm running into is that something is wrong with my geojson object and application is throwing the following error: Uncaught TypeError: Cannot read property 'length' of undefined
. Technically, since this is an object and not an array, it shouldn't have a length property, so I don't really know why it's throwing the error. The main difference seems to be that I have added "properties" attributes to the features in the geojson object.
var geojsonObject = {
"type": "FeatureCollection",
"crs": {"type": "name","properties": {"name": "EPSG:3857"},
"Features":[
{
"type" : "feature",
"geometry" : {"type" : "Point", "coordinates" : [-4.65, 79.36]},
"properties" : {
"name" : "ARED",
"country" : "Rwanda",
"sector" : "ITC"
}
},{
"type" : "feature",
"geometry" : {'type':'Point','coordinates': [8.08, 29.19]},
"properties" : {
"name" : "Bio Phyto Collines",
"country": "Benin",
"sector": "Organic Ag Inputs"
}
}
]}}
var vectorSource = new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(geojsonObject)
});
The error gets thrown on the readFeatures method when passed the geojson. I'm open to other implementations, but I was trying to something more elegant than creating each feature manually.
Upvotes: 0
Views: 930
Reputation: 1012
Check your geojsonObject
Object again. and please indent better with JSON type Object. I added comments on the wrong points in the code
var geojsonObject = {
"type": "FeatureCollection",
"crs": { // First, you didn't close `crs` brackets
"type": "name",
"properties": {
"name": "EPSG:3857"
}
},
"features":[{
"type" : "Feature", // Second, it's `Feature' not `feature`
"geometry" : {
"type" : "Point",
"coordinates" : [-4.65, 79.36]
},
"properties" : {
"name" : "ARED",
"country" : "Rwanda",
"sector" : "ITC"
}
}, {
"type" : "Feature", // Feature again.
"geometry" : {
'type':'Point',
'coordinates': [8.08, 29.19]
},
"properties" : {
"name" : "Bio Phyto Collines",
"country": "Benin",
"sector": "Organic Ag Inputs"
}
}]
};
Upvotes: 2