Openlayers 3 如何查找以前在功能上设置的样式文本-OL3
使用OL3,我在样式上动态设置文本:Openlayers 3 如何查找以前在功能上设置的样式文本-OL3,openlayers-3,Openlayers 3,使用OL3,我在样式上动态设置文本: var myLayer = new ol.layer.Vector({ source: mySource, style: function (feature, resolution) { var style = new ol.style.Style({ text: new ol.style.Text({
var myLayer = new ol.layer.Vector({
source: mySource,
style: function (feature, resolution) {
var style = new ol.style.Style({
text: new ol.style.Text({
text: setText(feature)
})
});
return [style];
}
});
我试图稍后阅读文本中存储的内容:
text: setText(feature)
我试图检索单击事件的文本,但不确定如何在功能样式下访问该属性(功能是包含单击功能的事件中的变量):
但是当我这样做的时候,我会得到一个null currentFeatureStyle
还尝试循环使用该功能:
for (var fid in feature)
{
//what to look for to extract the feature text?
map.on('singleclick', function (evt) {
var feature = map.forEachFeatureAtPixel(evt.pixel,
function (feature, layer) {
return feature;
});
// Here is the workaround step 2:
// Get "display text" custom field for this feature
var displayText = feature.displayText;
但不确定要寻找什么来提取特征文本。如果您能帮助从功能中获取功能文本,我们将不胜感激
谢谢如果有人能想出一个更正确的答案,请尽一切可能在下面发布。我已经创建了一个“变通方法”,因为我无法从功能中检索文本。解决方案包括以下内容:
function setText(feature)
{
// First do your normal stuff (set the text of the feature)
var output = "myFeatureText";
// Here is the workaround step 1:
// Create a property and set it to the text
feature.displayText = output;
// Return back value for the setting of the style text
return output;
}
function setText(feature)
{
// First do your normal stuff (set the text of the feature)
var output = "myFeatureText";
// Here is the workaround step 1:
// Create a property and set it to the text
feature.displayText = output;
// Return back value for the setting of the style text
return output;
}
for (var fid in feature)
{
//what to look for to extract the feature text?
map.on('singleclick', function (evt) {
var feature = map.forEachFeatureAtPixel(evt.pixel,
function (feature, layer) {
return feature;
});
// Here is the workaround step 2:
// Get "display text" custom field for this feature
var displayText = feature.displayText;