[英]Openlayers 3: add text label to feature
我在這里設置了當前設置:功能齊全的小提琴示例,雖然我已經設法縮放到每個多邊形特征,但我還想在每個多邊形特征上顯示一個集中的文本標簽......在get_fields
方法中找到的field_title
變量。 我不知道如何做到這一點,我所有的谷歌搜索都提出了這篇文章: http : //openlayers.org/en/v3.3.0/examples/vector-labels.html我覺得這完全令人困惑,因為我是一個OL 的小新人!
要向ol.Feature
添加文本,您將在特征中存儲描述並設置作為樣式函數的樣式(將從特征中獲取描述並顯示它):
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') : ''
})
})
];
}
由於我是新來的,不允許發表評論,我把我的評論作為@andre_ss6的問題的新答案。 我也得到Window
在this
。 對我有用的是將特征對象作為函數的第一個參數傳入:
function styleFunction(feature) {
然后使用該參數而不是this
:
text: feature.get('description')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.