我在OL地图上有几个图层,其中包含相同大小的要素的轮廓,背景颜色和标签,因此您可以显示或隐藏一个或多个图层.其中两个层只是标签......样式不包含填充或描边.一个标签应位于要素中心的另一个上方,但OL似乎相对于要素多边形的高度将它们垂直地远离或更靠近地展开,如下所示:
我尝试offsetY: 15在下标签的文本样式块中设置一个,在下标签之前添加换行符textBaseline:'top',在下标签和textBaseline:'bottom'顶部设置换行(这是最后一次尝试!)但它们总是以不同的方式展开!
这是我的上标签的样式块:
function fields_label_style() {
return [
new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(255,255,255,0)'
}),
stroke: new ol.style.Stroke({
color: 'rgba(255,255,255,0)',
width: 1
}),
text: new ol.style.Text({
font: '13px Calibri,sans-serif',
fill: new ol.style.Fill({ color: '#ffffff' }),
stroke: new ol.style.Stroke({
color: '#000000', width: 2
}),
// get the text from the feature - `this` is ol.Feature
// and show only under certain resolution
text: map.getView().getZoom() > 14 ? this.get('description') : ''
})
})
];
}
Run Code Online (Sandbox Code Playgroud)
对于较低的标签:
function cropping_label_style() {
return [
new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(255,255,255,0)'
}),
stroke: new ol.style.Stroke({
color: 'rgba(255,255,255,0)',
width: 1
}),
text: new ol.style.Text({
font: '13px Calibri,sans-serif',
fill: new ol.style.Fill({ color: '#ffffff' }),
stroke: new ol.style.Stroke({
color: '#000000', width: 2
}),
// get the text from the feature - `this` is ol.Feature
// and show only under certain resolution
text: map.getView().getZoom() > 14 ? this.get('description') : '',
offsetY: 15
})
})
];
}
Run Code Online (Sandbox Code Playgroud)
两者肯定具有相同的多边形轮廓.没有问题.我只能认为OpenLayers可能会将偏移量视为百分比而不是像文档中所述的像素:
由于代码中存在一些复杂的循环,我忽略了某些字段上的多边形边界之间存在细微的差异,这意味着它们的标签显示在略有不同的位置。现在我已经使所有多边形边界相同,标签确实与预期的行为正确对齐offsetY。我很抱歉。