[英]Openlayers 3: add text label to feature
I have the current set up here: fully functional fiddle example and whilst I have managed to zoom to each polygon feature I would also like to display a centralised text label on each... the field_title
variable found within the get_fields
method.我在这里设置了当前设置:功能齐全的小提琴示例,虽然我已经设法缩放到每个多边形特征,但我还想在每个多边形特征上显示一个集中的文本标签......在
get_fields
方法中找到的field_title
变量。 I have no idea how to do this and all my googling has come up with this article: http://openlayers.org/en/v3.3.0/examples/vector-labels.html which I find totally confusing as I'm a little new to OL!我不知道如何做到这一点,我所有的谷歌搜索都提出了这篇文章: http : //openlayers.org/en/v3.3.0/examples/vector-labels.html我觉得这完全令人困惑,因为我是一个OL 的小新人!
To add a text to ol.Feature
you will store the description in the feature andset a style that is a style function (that will get the description from the feature and show it):要向
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') : ''
})
})
];
}
Since I am new here and are not allowed to comment, I put my comment as a new answer to the question of @andre_ss6.由于我是新来的,不允许发表评论,我把我的评论作为@andre_ss6的问题的新答案。 I also get
Window
on this
.我也得到
Window
在this
。 What works for me is passing in the feature object as the function's first parameter:对我有用的是将特征对象作为函数的第一个参数传入:
function styleFunction(feature) {
and then use that parameter instead of this
:然后使用该参数而不是
this
:
text: feature.get('description')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.