微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我现在设置了这个: fully functional fiddle example,虽然我已经设法缩放到每个多边形特征,但我还想在每个…上显示中文标签…在get_fields方法中找到的field_title变量.我不知道如何做到这一点,所有我的谷歌搜索都提出了这篇文章http://openlayers.org/en/v3.3.0/examples/vector-labels.html我发现完全混乱,因为我对OL有点新!

解决方法

要向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,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.

原文地址:https://www.jb51.cc/js/151054.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐