简体   繁体   English

OpenLayers 3-在地图上显示文本,相对于缩放的文本大小

[英]OpenLayers 3 - Display text on map, text size relative to zoom

I'm currently working on OL3 and trying to display a text on my map. 我目前正在使用OL3,并试图在地图上显示文本。 No problem with that, I'm using the text property of ol.style to display it. 没问题,我正在使用ol.style的text属性显示它。

My problem is the following: this text has always the same size. 我的问题如下:此文本始终具有相同的大小。 ie: if I'm unzooming max or zooming max, it's still the same size on the screen. 即:如果我正在最大缩放或最大缩放,则它在屏幕上的大小仍然相同。 I want it to grow while zooming, and shrink while unzooming to keep the text's size relative to the ground. 我希望它在缩放时增大,而在不缩放时缩小,以保持文本相对于地面的大小。

I drew the expected result in the following image: Expected result 我在下图中绘制了预期的结果: 预期的结果

Is anyone having an idea? 有人有主意吗? Maybe without using a ol.style text? 也许不使用ol.style文本?

The way to go is a style function . 要走的路是样式功能 See demo fiddle . 参见演示小提琴

var style = function () {
  var zoom = map.getView().getZoom();
  var font_size = zoom * 10; // arbitrary value

  return [
    new ol.style.Style({
      text: new ol.style.Text({
        font: font_size + 'px Calibri,sans-serif',
        fill: new ol.style.Fill({ color: '#000' }),
        stroke: new ol.style.Stroke({
          color: '#fff', width: 2
        }),
        text: 'Some text!'
      })
    })
  ];
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM