Openlayers绘制带箭头的路线只用到了ol.FeatureStyleFunction,简单易懂,详细步骤及代码如下:
第一步,创建线要素:
var line_feature = new ol.Feature();
var line_geom=new ol.geom.LineString(paths);
line_feature.setGeometry(line_geom)
第二步,创建线图层并添加到地图对象中
var polyLineLayer = new ol.layer.Vector({
source: new ol.source.Vector({
features: [ine_feature]
}),
});
map.addLayer(polyLineLayer);
第三步,设置styleFunction,ol.FeatureStyleFunction只有一个参数resolution,详细代码如下:
var styles=function (resolution) {
var geometry = this.getGeometry();
var length = geometry.getLength();//获取线段长度
var radio = (50 * resolution) / length;
var dradio = 1;//投影坐标系,如3857等,在EPSG:4326下可以设置dradio=10000
var styles = [
new ol.style.Style({
stroke: new ol.style.Stroke({
color: "green",
width: 10,
})