我想使用OpenLayers给图标分配文本或标签。我可以将标签分配给图标,但它会将文本放在图标.but上,我要在偏移位置显示标签,以便图标和标签都能正确显示。我试过使用offsetY,但它不工作,也没有改变位置。
var styleToponimiFunction = function(feature,resolution) { console.log(feature); var iconName; if (feature.get("remark")=='Daerah Istimewa') { iconName='daerah_istimewa2.png'; else if (feature.get("remark")=='Kecamatan'){ iconName='kecamatan.png'; iconStyle = [new ol.style.Style({ image: new ol.style.Icon(({ anchor: [0.5, 46], anchorXUnits: "fraction", anchorYUnits: "pixel", scale:0.03, opacity:1, src: "icons/" + iconName, text: new ol.style.Text({ text: feature.get('namobj'), font: '12px Calibri,sans-serif', fill: new ol.style.Fill({ color: 'black' }), stroke: new ol.style.Stroke({ color: '#fff', width: 2, textAlign: 'center', Baseline: 'hanging', offsetX : 0, offsetY : -12, declutter: true, overflow: true return iconStyle; }
上云精选
2核2G云服务器 每月9.33元起,还有更多云产品低至0.02元
文本选项不应位于Stroke构造函数内,而且 Baseline 应为 textBaseline
Baseline
textBaseline
stroke: new ol.style.Stroke({ color: '#fff',