I'm trying to get the "id" of the layer for a selected feature and have tried maybe 3 or 4 methods for achieving this but have yet to achieve it.
I add my features like this...
angular.forEach(response.FieldList, function (Field, key) {
if (Field.FieldID != "") {
var shape = response.FieldList[key].Shape;
shape = shape.replace('}', ',"id":' + '"' + Field.FieldID + '"' + '}');
var geoJsonObj = {
'type': 'Feature',
'geometry': JSON.parse(shape),
'name': Field.FieldID,
'id': Field.FieldID
}
var vectorSource = new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(geoJsonObj)
});
Fields[Field.FieldID] = new ol.layer.Vector({
projection: 'EPSG:4269',
source: vectorSource,
id: Field.FieldID,
name: 'Fields',
style: function (feature, resolution) {
var text = resolution * 100000 < 10 ? response.FieldList[key].Acres : '';
if (text != "") {
styleCache[text] = [new ol.style.Style({
stroke: new ol.style.Stroke({
color: '#319FD3',
width: 1
}),
text: new ol.style.Text({
font: '12px Calibri,sans-serif',
text: text,
fill: new ol.style.Fill({
color: '#000'
}),
stroke: new ol.style.Stroke({
color: '#fff',
width: 3
})
}),
fill: new ol.style.Fill({
color: rcisWebMapUtilities.convertHex(response.FieldList[key].Shade, '0.5')
})
})];
}
else if (text == "") {
styleCache[text] = [new ol.style.Style({
fill: new ol.style.Fill({
color: rcisWebMapUtilities.convertHex(response.FieldList[key].Shade, '0.5')
})
})
]
} return styleCache[text];
}
});
webMapValues.vectorFieldLayer.push(Fields[Field.FieldID])
webMapValues.fieldValues.push({
color: response.FieldList[key].Shade,
plantingName: response.FieldList[key].CropNickName,
acres: response.FieldList[key].Acres,
cropId: response.FieldList[key].CropID,
cropNumber: response.FieldList[key].CropNumber,
fieldID: response.FieldList[key].FiledID,
fieldName: response.FieldList[key].FieldName,
legalDesc: response.FieldList[key].LegalDesc,
policyNum: response.FieldList[key].PolicyNumber
})
var found = $filter('filter')(webMapValues.legend, { plantingName: response.FieldList[key].CropNickName }, true);
if (found == 0) {
webMapValues.legend.push({
color: response.FieldList[key].Shade,
plantingName: response.FieldList[key].CropNickName
})
}
}
});
as you can see I'm trying to set the "id" in many places...even altering the GeoJSON to include 'id' but it seems to get discarded somehow and is not there when I want to use it?
I am using a map.on 'click" like this...
map.on('click', function (evt) {
var pixel = map.getEventPixel(evt.originalEvent);
displayFeatureInfo(evt.pixel, evt.coordinate);
//var coordinate = evt.coordinate;
})
and this code to perform the highlight...
var highlight;
var displayFeatureInfo = function (pixel,coordinate) {
var feature = map.forEachFeatureAtPixel(pixel, function (feature) {
var id = Opelayers magic to get layer id;
return feature;
});
var info = document.getElementById('info');
if (feature) {
info.innerHTML = feature.getId() + ': ' + feature.get('name');
} else {
info.innerHTML = ' ';
}
if (feature !== highlight) {
if (highlight) {
featureOverlay.getSource().removeFeature(highlight);
}
if (feature) {
featureOverlay.getSource().addFeature(feature);
document.getElementById('popup-content').innerHTML = '<p>It is working</p>';
popup.setPosition(coordinate);
}
highlight = feature;
}
};
feature.getId()
and feature.get('name')
return undefined?
after I get the feature I would like to get the "id" of the layer it is on. so probably in this code...
var feature = map.forEachFeatureAtPixel(pixel, function (feature) {
var id = Opelayers magic to get layer id;
return feature;
});
Is this possible? any help is greatly appreciated!!
id
for each one withol.Feature#setId
– Abid
from your object. You'll have to useol.Feature#setId
. Looking at your code it's hard to understand why you create a newol.source.Vector
and a newol.layer.Vector
inside a loop. – Ab