在 Openlayers 6 中将静态文本设置为矢量
Set static Text to Vector in Openlayers 6
我正在尝试将“Epicenter”设置为创建的 Vector 我正在使用 Text
、Fill
和 Stroke
作为 Style
然后发送此 style
到我的矢量图,但它什么也没显示。
const epicenterStyle = new ol.style.Style({
text: new ol.style.Text({
font: '12px Calibri,sans-serif',
fill: new ol.style.Fill({
color:'#000'
}),
stroke: new ol.style.Stroke({
color:'#fff',
width:3
}),
textAlign:'left',
offsetX:2
})
});
pointEarthquake = new ol.layer.Vector({
source: new ol.source.Vector({
features: [position]
}),
visible: true,
title: "Evento sísmico",
style: epicenterStyle.setText('Epicenter')
})
也试过 Vector style: epicenterStyle.getText().setText('Epicenter')
如果我 console.log(epicenterStyle)
我得到这个:
setText()
更新样式,但不 return 样式
先调用setText()
,然后使用更新后的样式
epicenterStyle.getText().setText('Epicenter');
pointEarthquake = new ol.layer.Vector({
source: new ol.source.Vector({
features: [position]
}),
visible: true,
title: "Evento sísmico",
style: epicenterStyle
})
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.9.0/css/ol.css" type="text/css">
<style>
html, body, .map {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
</style>
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.9.0/build/ol.js"></script>
</head>
<body>
<div id="map" class="map"></div>
<script type="text/javascript">
const map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
view: new ol.View({
center: [0, 0],
zoom: 4
})
});
const position = new ol.Feature(new ol.geom.Point([0, 0]));
const epicenterStyle = new ol.style.Style({
text: new ol.style.Text({
font: '12px Calibri,sans-serif',
fill: new ol.style.Fill({
color:'#000'
}),
stroke: new ol.style.Stroke({
color:'#fff',
width:3
}),
textAlign:'left',
offsetX:2
})
});
epicenterStyle.getText().setText('Epicenter');
const pointEarthquake = new ol.layer.Vector({
source: new ol.source.Vector({
features: [position]
}),
visible: true,
title: "Evento sísmico",
style: epicenterStyle
});
map.addLayer(pointEarthquake);
</script>
</body>
</html>
我正在尝试将“Epicenter”设置为创建的 Vector 我正在使用 Text
、Fill
和 Stroke
作为 Style
然后发送此 style
到我的矢量图,但它什么也没显示。
const epicenterStyle = new ol.style.Style({
text: new ol.style.Text({
font: '12px Calibri,sans-serif',
fill: new ol.style.Fill({
color:'#000'
}),
stroke: new ol.style.Stroke({
color:'#fff',
width:3
}),
textAlign:'left',
offsetX:2
})
});
pointEarthquake = new ol.layer.Vector({
source: new ol.source.Vector({
features: [position]
}),
visible: true,
title: "Evento sísmico",
style: epicenterStyle.setText('Epicenter')
})
也试过 Vector style: epicenterStyle.getText().setText('Epicenter')
如果我 console.log(epicenterStyle)
我得到这个:
setText()
更新样式,但不 return 样式
先调用setText()
,然后使用更新后的样式
epicenterStyle.getText().setText('Epicenter');
pointEarthquake = new ol.layer.Vector({
source: new ol.source.Vector({
features: [position]
}),
visible: true,
title: "Evento sísmico",
style: epicenterStyle
})
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.9.0/css/ol.css" type="text/css">
<style>
html, body, .map {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
</style>
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.9.0/build/ol.js"></script>
</head>
<body>
<div id="map" class="map"></div>
<script type="text/javascript">
const map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
view: new ol.View({
center: [0, 0],
zoom: 4
})
});
const position = new ol.Feature(new ol.geom.Point([0, 0]));
const epicenterStyle = new ol.style.Style({
text: new ol.style.Text({
font: '12px Calibri,sans-serif',
fill: new ol.style.Fill({
color:'#000'
}),
stroke: new ol.style.Stroke({
color:'#fff',
width:3
}),
textAlign:'left',
offsetX:2
})
});
epicenterStyle.getText().setText('Epicenter');
const pointEarthquake = new ol.layer.Vector({
source: new ol.source.Vector({
features: [position]
}),
visible: true,
title: "Evento sísmico",
style: epicenterStyle
});
map.addLayer(pointEarthquake);
</script>
</body>
</html>