超出标记区域时文本消失 Open Layer
text disappear when goes outside marked area Open Layer
在标记区域内我写了一些文字。当我缩小时,文本会出现在区域之外并消失。有什么办法可以防止这种情况吗?
我试图根据缩放大小更改文本大小,以便它始终在区域内匹配并且永远不会消失,但它只会捕捉一次缩放,之后不会更改任何内容。
下面是我的部分代码。
var styleText = function(text) {
var zoom = map.getView().getZoom();
var resolution = map.getView().getResolution();
var font = zoom * 3
return {
text: new ol.style.Text({
font: font+'px Courier New',
fill: new ol.style.Fill({
color: '#ffd300'
}),
stroke: new ol.style.Stroke({
color: '#000',
width: 3
}),
textAlign: "center",
textBaseline: "middle",
text: text,
})
}
}
var newVector = new ol.layer.Vector({
name: areaIncra.farmId,
source: kmlVector,
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'red',
width: 3
}),
text: styleText('Text to show').text
})
});
要重新计算样式,它需要是一个函数
style: function(feature, resolution) { return new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'red',
width: 3
}),
text: styleText('Text to show').text
}); }
您可能还想在 ol.style.Text
https://openlayers.org/en/v4.6.5/apidoc/ol.style.Text.html
上设置 overflow: true
var styleText = function(text) {
var zoom = map.getView().getZoom();
var resolution = map.getView().getResolution();
var font = (zoom + 1) * 3;
return {
text: new ol.style.Text({
font: font+'px Courier New',
fill: new ol.style.Fill({
color: '#ffd300'
}),
stroke: new ol.style.Stroke({
color: '#000',
width: 3
}),
textAlign: "center",
textBaseline: "middle",
text: text,
overflow: true
})
}
}
var newVector = new ol.layer.Vector({
source: new ol.source.Vector(),
style: function(feature, resolution) {
return new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'red',
width: 3
}),
text: styleText('Text to show').text
});
}
});
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM(),
}),
newVector
],
target: 'map',
view: new ol.View({
center: [0, 0],
zoom: 2
})
});
map.addInteraction(new ol.interaction.Draw({
source: newVector.getSource(),
type: 'Polygon',
}));
<link href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" rel="stylesheet" />
<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
<div id="map" class="map"></div>
在标记区域内我写了一些文字。当我缩小时,文本会出现在区域之外并消失。有什么办法可以防止这种情况吗?
我试图根据缩放大小更改文本大小,以便它始终在区域内匹配并且永远不会消失,但它只会捕捉一次缩放,之后不会更改任何内容。
下面是我的部分代码。
var styleText = function(text) {
var zoom = map.getView().getZoom();
var resolution = map.getView().getResolution();
var font = zoom * 3
return {
text: new ol.style.Text({
font: font+'px Courier New',
fill: new ol.style.Fill({
color: '#ffd300'
}),
stroke: new ol.style.Stroke({
color: '#000',
width: 3
}),
textAlign: "center",
textBaseline: "middle",
text: text,
})
}
}
var newVector = new ol.layer.Vector({
name: areaIncra.farmId,
source: kmlVector,
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'red',
width: 3
}),
text: styleText('Text to show').text
})
});
要重新计算样式,它需要是一个函数
style: function(feature, resolution) { return new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'red',
width: 3
}),
text: styleText('Text to show').text
}); }
您可能还想在 ol.style.Text
https://openlayers.org/en/v4.6.5/apidoc/ol.style.Text.html
overflow: true
var styleText = function(text) {
var zoom = map.getView().getZoom();
var resolution = map.getView().getResolution();
var font = (zoom + 1) * 3;
return {
text: new ol.style.Text({
font: font+'px Courier New',
fill: new ol.style.Fill({
color: '#ffd300'
}),
stroke: new ol.style.Stroke({
color: '#000',
width: 3
}),
textAlign: "center",
textBaseline: "middle",
text: text,
overflow: true
})
}
}
var newVector = new ol.layer.Vector({
source: new ol.source.Vector(),
style: function(feature, resolution) {
return new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'red',
width: 3
}),
text: styleText('Text to show').text
});
}
});
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM(),
}),
newVector
],
target: 'map',
view: new ol.View({
center: [0, 0],
zoom: 2
})
});
map.addInteraction(new ol.interaction.Draw({
source: newVector.getSource(),
type: 'Polygon',
}));
<link href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" rel="stylesheet" />
<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
<div id="map" class="map"></div>