Reputation: 41
I am using Openlayers 3 and would like to create a graph real time from information contained on a selected object. I need to access the properties, but get('myfield') is not working. My features are in a GeoJSON vector layer.
var selectSingleClick = new ol.interaction.Select();
map.addInteraction(selectSingleClick);
map.on('singleclick', function(event){
mylayer.once('precompose',function(event){
var selectedFeatures = selectSingleClick.getFeatures();
readFeature(selectedFeatures);
});
});
function readFeature(features){
consoleText = document.getElementById('console');
// When selected, getLength() returns 1, so selection is working.
// consoleText.innerHTML = features.getLength();
var myfeature = features[0];
consoleText.innerHTML += myfeature.get('objectId');
}
Anybody can help me to understand what is going wrong? I have not much experience with Javascript.
Upvotes: 2
Views: 1170
Reputation: 1159
If u look at the documentation of openlayers 3
http://openlayers.org/en/latest/apidoc/ol.interaction.Select.html#getFeatures
selectSingleClick.getFeatures()
returns ol.Collection() object which is an enhancement over normal Javascript array object.
For more information on ol.Collection() have a look at this link.
http://openlayers.org/en/latest/apidoc/ol.Collection.html
var myfeature = features[0];
is normal way to get an object from an array.
Upvotes: 1
Reputation: 41
I have found my problem. Trying to access the features by
var myfeature = features[0];
Correct syntax should be:
var myfeature = features.item(0);
But in previous examples features[0] have worked. Would be keen to understand why this is so...
Upvotes: 2