Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 845
Yoann B Avatar asked Dec 01 '25 17:12

Yoann B


1 Answers

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!'
      })
    })
  ];
};
like image 50
Jonatas Walker Avatar answered Dec 04 '25 06:12

Jonatas Walker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!