如何在 Openlayers3 中编辑选定的文本?

How can I edit the selected text in Openlayers3?

我现在在实习,第一次接触OL3。 我尝试制作一个演示来使用标记绘制和更改文本。 但是我发现我只能更改标记的所有文本,而不能更改选定的文本。 这是我做的一个简单的jsfiddle

  features: select.getFeatures()

我认为这对我不起作用。 那我该怎么做呢?

您可以将 style 变量更改为 ol.FeatureStyleFunction() 以从存储在要素中的 属性 获取标签。

var style = function () {
  return [
    new ol.style.Style({
      image: new ol.style.Icon({
        anchor: [0.5, 46],
        anchorXUnits: 'fraction',
        anchorYUnits: 'pixels',
        opacity: 0.75,
        src: '//openlayers.org/en/v3.8.2/examples/data/icon.png'
      }),
      text: new ol.style.Text({
        font: '12px Calibri,sans-serif',
        fill: new ol.style.Fill({ color: '#000' }),
        stroke: new ol.style.Stroke({
          color: '#fff', width: 2
        }),
        // get the text from the feature (`this` is the feature)
        text: this.get('text')
      })
    })
  ];
};

要更新文本,请更新此 属性:

var features = select.getFeatures();
features.forEach(function(feature){
  feature.set('text', nameElement);
});

http://jsfiddle.net/jonataswalker/b44nxco8/