私はOpenLayersの初心者ですが、次のようなより複雑なグラフィカルオブジェクトを描画する方法を考えています。
私は彼らの例を見ましたが、そのような図面は見つかりませんでした。画像ではなく、プリミティブを使用したいと思います。
更新:
onDrawEnd (event) {
this.sizeFeature = event.feature
this.$refs.setSizeDialog.open()
},
onSizeSet (title) {
this.sizeFeature.set('graphics', true)
this.sizeFeature.set('style', sizeStyleId)
this.sizeFeature.set('title', title)
this.setFeatureStyle(this.sizeFeature)
// Save graphics after the line with title was drawn
developedDocumentsApi.saveDrawingGraphics(this.document.id, this.updateGraphicsObjList())
}
setFeatureStyle (feature) {
const styleId = feature.get('style')
let style = null
switch (styleId) {
case 0: {
style = this.getRedPoint()
break
}
...
case 11: {
const title = feature.get('title')
style = this.getSizeStyle(feature, title)
break
}
}
feature.setStyle(style)
}
getSizeStyle (feature, title) {
const pointStyle = new Style({
image: new Circle({
radius: width * 2,
fill: new Fill({ color: 'black' }),
stroke: new Stroke({ color: 'black', width: width / 2 })
}),
zIndex: Infinity
})
const lineStyle = new Style({
stroke: new Stroke({ color: 'black', width: 2 }),
text: new Text({
font: '12px Calibri,sans-serif',
overflow: true,
placement: 'line',
textBaseline: 'bottom',
fill: new Fill({ color: 'black' }),
stroke: new Stroke({ color: '#fff', width: 3 })
})
})
const startStyle = lineStyle.clone()
const endStyle = lineStyle.clone()
const resolution = this.devDocMap.getView().getResolution()
const styles = [pointStyle]
const geometry = feature.getGeometry()
if (geometry.getType() === 'LineString') {
console.log('LineString')
lineStyle.getText().setText((feature.getGeometry().getLength() / 1000).toFixed())
styles.push(lineStyle)
const pixels = 10
const start = geometry.getFirstCoordinate()
startStyle.setGeometry(LineString([[start[0], start[1] - pixels * resolution], [start[0], start[1] + pixels * resolution]]))
styles.push(startStyle)
const end = geometry.getLastCoordinate()
endStyle.setGeometry(LineString([[end[0], end[1] - pixels * resolution], [end[0], end[1] + pixels * resolution]]))
styles.push(endStyle)
return styles
}
}
この例https://openlayers.org/en/v4.6.5/examples/line-arrows.htmlと同様のことを行うことができますが、アイコン画像を使用する代わりに、行末を線文字列としてスタイル設定する必要があります
var raster = new ol.layer.Tile({
source: new ol.source.OSM()
});
var source = new ol.source.Vector();
var white = [255, 255, 255, 1];
var blue = [0, 153, 255, 1];
var width = 3;
var pointStyle = new ol.style.Style({
image: new ol.style.Circle({
radius: width * 2,
fill: new ol.style.Fill({
color: blue
}),
stroke: new ol.style.Stroke({
color: white,
width: width / 2
})
}),
zIndex: Infinity
});
var lineStyle = new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'black',
width: 2
}),
text: new ol.style.Text({
font: '12px Calibri,sans-serif',
overflow: true,
placement: 'line',
textBaseline: 'bottom',
fill: new ol.style.Fill({
color: 'black'
}),
stroke: new ol.style.Stroke({
color: '#fff',
width: 3
})
})
});
var startStyle = lineStyle.clone();
var endStyle = lineStyle.clone();
var styleFunction = function(feature, resolution) {
var styles = [pointStyle];
var geometry = feature.getGeometry();
if (geometry.getType() == 'LineString') {
lineStyle.getText().setText((feature.getGeometry().getLength()/1000).toFixed());
styles.push(lineStyle);
var pixels = 10;
var start = geometry.getFirstCoordinate();
startStyle.setGeometry(new ol.geom.LineString([[start[0], start[1] - pixels*resolution], [start[0], start[1] + pixels*resolution]]));
styles.push(startStyle);
var end = geometry.getLastCoordinate();
endStyle.setGeometry(new ol.geom.LineString([[end[0], end[1] - pixels*resolution], [end[0], end[1] + pixels*resolution]]));
styles.push(endStyle);
}
return styles;
};
var vector = new ol.layer.Vector({
source: source,
style: styleFunction
});
var map = new ol.Map({
layers: [raster, vector],
target: 'map',
view: new ol.View({
center: [-11000000, 4600000],
zoom: 4
})
});
map.addInteraction(new ol.interaction.Draw({
source: source,
type: 'LineString',
style: styleFunction
}));
html, body, .map {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
<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>
あなたの場合getSizeStyle
はスタイル関数を返す必要がありますが、解像度によって変化するため、静的スタイルを使用することはできません。
getSizeStyle (feature, title) {
const pointStyle = new Style({
image: new Circle({
radius: width * 2,
fill: new Fill({ color: 'black' }),
stroke: new Stroke({ color: 'black', width: width / 2 })
}),
zIndex: Infinity
})
const lineStyle = new Style({
stroke: new Stroke({ color: 'black', width: 2 }),
text: new Text({
font: '12px Calibri,sans-serif',
overflow: true,
placement: 'line',
textBaseline: 'bottom',
fill: new Fill({ color: 'black' }),
stroke: new Stroke({ color: '#fff', width: 3 })
})
})
const startStyle = lineStyle.clone()
const endStyle = lineStyle.clone()
return function(feature, resolution) {
const styles = [pointStyle]
const geometry = feature.getGeometry()
if (geometry.getType() === 'LineString') {
console.log('LineString')
lineStyle.getText().setText((feature.getGeometry().getLength() / 1000).toFixed())
styles.push(lineStyle)
const pixels = 10
const start = geometry.getFirstCoordinate()
startStyle.setGeometry(new LineString([[start[0], start[1] - pixels * resolution], [start[0], start[1] + pixels * resolution]]))
styles.push(startStyle)
const end = geometry.getLastCoordinate()
endStyle.setGeometry(new LineString([[end[0], end[1] - pixels * resolution], [end[0], end[1] + pixels * resolution]]))
styles.push(endStyle)
return styles
}
}
}
title
使用されていないようでpointStyle
、ポイント機能を使用してマウスの位置を強調表示する描画インタラクションでのみ必要です。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加