Yoann B
Yoann B

Reputation: 123

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. No problem with that, I'm using the text property of ol.style to display it.

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?

Upvotes: 3

Views: 4932

Answers (1)

Jonatas Walker
Jonatas Walker

Reputation: 14150

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

Upvotes: 5

Related Questions