简体   繁体   English

带有标签或文本的图标在openlayers 3中,偏移

[英]icon with label or text in openlayers 3, offset

I need to add an icon and add a text on botton of the image. 我需要添加一个图标并在图像的底部添加一个文本。 How can I do this ? 我怎样才能做到这一点 ?

I tried with this styles, but the text is rendered in the middle of the image. 我尝试使用这种样式,但文本在图像中间呈现。

var vector = new ol.layer.Vector({
      source: new ol.source.Vector({
        projection: ol.proj.get('EPSG:4326')
      }),
      style: new ol.style.Style({rules: [
        new ol.style.Rule({
          symbolizers: [
            new ol.style.Icon({
                url: 'http://127.0.0.1/app/img/imageTest.png',
                opacity: 0.75,
                width: 12,
                height: 12
            }),
            new ol.style.Text({
                color: '#000',
                text: ol.expr.parse('i'),
                fontFamily: 'Calibri,sans-serif',
                fontSize: 12
            })
          ]
        })
      ]})
    });
map.addLayer(vector);

var f = new ol.Feature({
    'i': 1,
    'size': 20
});
f.setGeometry( new ol.geom.Point([lon,lat]) );

var features = new Array();
features.push(f);
vector.addFeatures(features);

I am using this code with a Canvas and it works with OL v3.0.0-beta.5: 我在Canvas中使用此代码,它适用于OL v3.0.0-beta.5:

 function getTextStyle(text, offsetX) {
    return new ol.style.Text({
      fill : new ol.style.Fill({
        color : '#330'
      }),
      stroke : new ol.style.Stroke({
        color : '#fff',
        width : 4
      }),
      text : text,
      font : '12px Verdana',
      offsetX : offsetX ? offsetX : 0,
      offsetY : 12
    });
  }

Also see what is now an 'experimental' documentation: http://ol3js.org/en/master/apidoc/ol.style.Text.html (edit: it was experimental in 2014 ...) 另请参阅现在的“实验性”文档: http//ol3js.org/en/master/apidoc/ol.style.Text.html (编辑:它在2014年进行了实验......)

You need to move the label by using "labelYOffset". 您需要使用“labelYOffset”移动标签。

If your graphic is 20px high, try shifting it down by 12px 如果您的图形高20px,请尝试将其向下移动12px

        new ol.style.Text({
            color: '#000',
            fontFamily: 'Calibri,sans-serif',
            fontSize: 12,
            label:"${NAME}",
            labelYOffset: -12
        })

The "label" parameter draws from the NAME field in an external file. “label”参数从外部文件中的NAME字段中提取。 The label align parameter is usually used in a style or style map. label align参数通常用在样式或样式映射中。 You will need to adjust this to reflect where your data is coming from. 您需要对其进行调整以反映数据的来源。
Note that labelXOffset and labelYoffset are not supported in the Canvas renderer. 请注意,Canvas渲染器不支持labelXOffset和labelYoffset。 So you need to ensure that your layer is using SVG for this to work. 因此,您需要确保您的图层使用SVG才能正常工作。 eg. 例如。

var point = new OpenLayers.Layer.Vector("GeoJSON", {
    strategies: [new OpenLayers.Strategy.Fixed()],
    styleMap: mystyle,
    protocol: new OpenLayers.Protocol.HTTP({
        url: "kml/my.geojson",
        format: new OpenLayers.Format.GeoJSON()
    }),
    renderers: ["SVG"]
});

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

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