2016-08-17 106 views
5

我目前在這裏設置:fully functional fiddle example,同時我設法縮放到每個多邊形功能我也想在每個顯示集中文本標籤...找到field_title變量在get_fields方法內。我不知道如何做到這一點,所有我的谷歌搜索已經拿出這篇文章:http://openlayers.org/en/v3.3.0/examples/vector-labels.html,我覺得完全混淆,因爲我是一個新的OL!Openlayers 3:添加文本標籤功能

+0

您可以將動畫部分移至另一個問題嗎?它可以幫助最初的混亂。 –

+0

@JonatasWalker完成了。謝謝。 –

回答

9

爲了增加ol.Feature你將存儲在特徵和描述set a style這是一個style function文本(也將獲得從特徵描述和表現出來):

field_polygon.set('description', field_title); 
field_polygon.setStyle(styleFunction); 

function styleFunction() { 
    return [ 
    new ol.style.Style({ 
     fill: new ol.style.Fill({ 
     color: 'rgba(255,255,255,0.4)' 
     }), 
     stroke: new ol.style.Stroke({ 
     color: '#3399CC', 
     width: 1.25 
     }), 
     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 ol.Feature 
     // and show only under certain resolution 
     text: map.getView().getZoom() > 12 ? this.get('description') : '' 
     }) 
    }) 
    ]; 
} 

Your fiddle

+0

這太棒了!謝謝!我如何只顯示特定縮放級別以上的文字? –

+1

查看更新的答案。 'map.getView()。getZoom()> 12? this.get('description'):''' –

+0

完美,謝謝! –