简体   繁体   English

openlayers 3群集中的字体大小

[英]Font Size in openlayers 3 cluster

I am using the new openlayers 3 api for showing a large amount of markers. 我正在使用新的openlayers 3 API来显示大量标记。 I extended the cluster example ( http://openlayers.org/en/v3.0.0/examples/cluster.html ). 我扩展了群集示例( http://openlayers.org/en/v3.0.0/examples/cluster.html )。

Everything works fine except one thing: The attribute fontSize in the style of the text is simply ignored. 除了一件事情之外,其他所有东西都工作正常:只需忽略文本样式中的fontSize属性。 The size of the text inside the cluster marker is always the same. 群集标记内的文本大小始终相同。

Is this a bug in openlayers 3 or am i doing something wrong? 这是openlayers 3中的错误还是我做错了什么? I tested it in the 3.0.0 release version and in the current development version of ol3. 我在3.0.0发行版和ol3的当前开发版本中对其进行了测试。

    var clusters = new $wnd.ol.layer.Vector({
        source: clusterSource,
        style: function (feature, resolution) {
            var size = feature.get('features').length;

            var style = styleCache[size];
            var radius = size + 10;
            if (radius > 25) {
                radius = 25;
            }

            if (!style) {
                style = [new $wnd.ol.style.Style({
                    image: new $wnd.ol.style.Circle({
                        radius: radius,
                        stroke: new $wnd.ol.style.Stroke({
                            color: '#fff'
                        }),
                        fill: new $wnd.ol.style.Fill({
                            color: color
                        })
                    }),
                    text: new $wnd.ol.style.Text({
                        text: size == 1 ? "●" : size.toString(),
                        fontSize: radius * 2 - 5,
                        fill: new $wnd.ol.style.Fill({
                            color: textColor
                        })
                    })
                })];
                styleCache[size] = style;
            }

            return style;
        }
    });

There's no fontSize option for ol.style.Text . ol.style.Text没有fontSize选项。 There's a font option though. 虽然有font选项。

The font option is a string , which has the same syntax as the Canvas context font attribute. font选项是一个string ,其语法与Canvas上下文font属性相同。 The default is '10px sans-serif' .See https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Drawing_text . 默认值为'10px sans-serif'参见https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Drawing_text

So in your case you'll use something like this: 因此,在您的情况下,您将使用以下内容:

var textStyle = new ol.style.Text({
  font: (radius * 2 - 5) + 'px sans-serif',
  // …
});

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

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