我通过使用 LineString 几何使用 openlayer v5 和样式功能,没有不同的不同样式。造型特征码:
applyStyle(feature,resolution){
var styleArray = [
new ol.style.Style({ // feature style
stroke: new ol.style.Stroke({
color: 'green',
width: 6,
lineCap: 'square'
}),
text: new Text({
text: 'gas',
textAlign: 'start',
offsetX: 1,
offsetY: 1,
fill: new Fill({
color: 'white'
})
})
}),
new ol.style.Style({ //feature sub style geometry
geometry: function (feature) {
var line = feature.getGeometry();
var coords = [];
line.forEachSegment(function (from, to) {
// for each segment calculate a parallel segment with a
// distance of 4 pixels
var angle = Math.atan2(to[1] - from[1], to[0] - from[0]);
var dist = 5 * resolution;
var newFrom = [
Math.sin(angle) * dist + from[0],
-Math.cos(angle) * dist + from[1]
];
var newTo = [
Math.sin(angle) * dist + to[0],
-Math.cos(angle) * dist + to[1]
];
coords.push(newFrom);
coords.push(newTo);
});
return new ol.geom.LineString(coords);
},
stroke: new ol.style.Stroke({
color: 'red',
width: 5,
lineCap: 'square'
}),
text: new Text({
text: 'redtext',
textAlign: 'start',
offsetX: 1,
offsetY: 1,
fill: new Fill({
color: 'white'
})
})
})
];
feature.setStyle(styleArray);
}
this.map.on('singleclick', function (evt: any) {
console.log("single click");
var features: any = self.map.getFeaturesAtPixel(evt.pixel);
features.forEach(feature => {
var styles = feature.getStyle();
delete styles[0]; //deleting feature style from array only
console.log();
styles.forEach(style => {
console.log();
var line: any = style.getGeometry();
var sss = style.getLineString(); //not working
var lineString = line.getLineString(); //not working
var geometry = style.getCoordinates(); //not working
console.log();
})
});
});
在地图单击事件的上述代码中,我尝试了 getGeometry() 但它没有返回几何或坐标。
单击即可获得子样式几何图形的任何帮助。