Я пытаюсь получить «id» слоя для выбранной функции и попробовал, возможно, 3 или 4 метода для достижения этого, но еще не достиг этого.openlayers 3 получить слой для выбранной функции
добавить свои функции, как это ...
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
})
}
}
});
, как вы можете видеть, что я пытаюсь установить «идентификатор» во многих местах ... даже изменения в формате GeoJSON включить «идентификатор», но кажется, что его отбрасывают как-то, а его нет, когда я хочу его использовать?
Я использую map.on «нажмите кнопку», как это ...
map.on('click', function (evt) {
var pixel = map.getEventPixel(evt.originalEvent);
displayFeatureInfo(evt.pixel, evt.coordinate);
//var coordinate = evt.coordinate;
})
и этот код для выполнения блика ...
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()
и feature.get('name')
возвращения неопределенного?
после того, как я получу функцию, я хотел бы получить «идентификатор» слоя, на котором он находится. так, вероятно, в этом коде ...
var feature = map.forEachFeatureAtPixel(pixel, function (feature) {
var id = Opelayers magic to get layer id;
return feature;
});
Возможно ли это? любая помощь очень ценится !!
Как вы добавляете функции к источнику? Вам нужно будет установить «id» для каждого из них с помощью ['ol.Feature # setId'] (http://openlayers.org/en/master/apidoc/ol.Feature.html#setId) –
Спасибо за ответ Джонатас ... Я пришел к такому же выводу, но изо всех сил пытаюсь понять, как правильно добавить «id» ... Я отредактирую свой вопрос с помощью соответствующего кода. –
OpenLayers не читает (намеренно) 'id' из вашего объекта. Вам нужно будет использовать 'ol.Feature # setId'. Глядя на ваш код, трудно понять, почему вы создаете новый «ol.source.Vector» и новый 'ol.layer.Vector' внутри цикла. –