Openlayers 3:为功能添加文本标签

Hel*_*rns 8 javascript openlayers-3

我在这里设置了当前设置:功能齐全的小提琴示例,虽然我已设法缩放到每个多边形功能,但我还想在每个...上显示集中文本标签... field_titleget_fields方法中找到的变量.我不知道如何做到这一点,我的所有谷歌搜索都提出了这篇文章:http://openlayers.org/en/v3.3.0/examples/vector-labels.html我发现这完全令人困惑,因为我是一个OL的新鲜事!

Jon*_*ker 15

要向您添加文本,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') : ''
      })
    })
  ];
}
Run Code Online (Sandbox Code Playgroud)

你的小提琴.

  • 你确定这有效吗?我在`this`上得到`Window`而不是`ol.Feature` (2认同)