2015-11-02 36 views
1

我現在正在做實習,這是我第一次接觸OL3。 我嘗試使用標記進行繪製和更改文本的演示。 但我發現我只能更改標記的所有文本,而不是更改選定的文本。 這是我製作的一個簡單的jsfiddle如何編輯Openlayers3中的選定文本?

features: select.getFeatures() 

這不適合我,我想。 那麼我該怎麼做?

+0

所以你會使用'ol.interaction.Select',對不對? –

+0

@GhitaB有一些編輯是根本! –

回答

0

您可以將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/

+0

謝謝,先生!這對我來說是完美的。我的同學和我被卡住了幾天:) –

+0

不客氣。祝你有美好的一天。 –

+0

你好,先生,抱歉再次打擾你。當我嘗試導入存在的圖層時,遇到了一個新問題。 text:this.get('text'):因爲這個函數在樣式定義中創建,這意味着如果我存儲到數據庫並重新加載它。文本的價值應該是固定的,它不應該是一種方法。如果我想修改從數據庫重新加載的現有對象,它不起作用。那我該怎麼辦? –

相關問題