Openlayers 3:向功能添加文本标签 [英] Openlayers 3: add text label to feature

查看:1507
本文介绍了Openlayers 3:向功能添加文本标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有当前设置:功能齐全的小提琴示例缩放到每个多边形要素我还想在每个多边形要素上显示一个集中的文本标签...在get_fields方法中找到的field_title变量.我不知道如何执行此操作,因此我所有的搜索都与这篇文章有关:

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. 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!

推荐答案

要将文本添加到ol.Feature,您将说明存储在功能部件中,并

To add a text to ol.Feature you will store the description in the feature and set a style that is a style function (that will get the description from the feature and show it):

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') : ''
      })
    })
  ];
}

您的小提琴 .

这篇关于Openlayers 3:向功能添加文本标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆