openlayers3带箭头的线

本文介绍如何使用OpenLayers库在地图上绘制带有箭头的线段,通过自定义样式函数实现箭头的动态生成,适用于多线段路径。文章提供详细代码示例,包括如何旋转箭头以匹配线段方向。

http://openlayers.org/en/latest/examples/line-arrows.html   --官网的例子

https://stackoverflow.com/questions/41606206/draw-arrow-without-using-any-image-in-openlayers3

https://stackoverflow.com/questions/40787691/openlayers-3-draw-arrow-on-multi-line

var source = new ol.source.Vector();

var styleFunction = function (feature) {
    var geometry = feature.getGeometry();
    var styles = [
        // linestring
        new ol.style.Style({
            stroke: new ol.style.Stroke({
                color: '#ffcc33',
                width: 2
            })
        })
    ];

    geometry.forEachSegment(function (start, end) {
        var dx = end[0] - start[0];
        var dy = end[1] - start[1];
        var rotation = Math.atan2(dy, dx);

        var lineStr1 = new ol.geom.LineString([end, [end[0] - 200000, end[1] + 200000]]);
        lineStr1.rotate(rotation, end);
        var lineStr2 = new ol.geom.LineString([end, [end[0] - 200000, end[1] - 200000]]);
        lineStr2.rotate(rotation, end);

        var stroke = new ol.style.Stroke({
            color: 'green',
            width: 1
        });

        styles.push(new ol.style.Style({
            geometry: lineStr1,
            stroke: stroke
        }));
        styles.push(new ol.style.Style({
            geometry: lineStr2,
            stroke: stroke
        }));
    });

    return styles;
};

var map = new ol.Map({
    layers: [
        new ol.layer.Tile({
            source: new ol.source.OSM()
        }),
        new ol.layer.Vector({
            source: source,
            style: styleFunction
        })
    ],
    target: 'map',
    view: new ol.View({
        center: [0, 0],
        zoom: 3
    })
});

map.addInteraction(new ol.interaction.Draw({
    source: source,
    type: ('LineString')
}));

<script src="https://openlayers.org/en/v3.20.1/build/ol.js"></script>
<link href="https://openlayers.org/en/v3.20.1/css/ol.css" rel="stylesheet"/>
<div id="map" class="map" tabindex="0"></div>

 

var styleFunction = function(feature) {
  var geometry = feature.getGeometry();

    var styles = [
    // linestring
    new ol.style.Style({
      stroke: new ol.style.Stroke({
        color: '#ffcc33',
        width: 2
      })
    })
  ];

var lineStringsArray = geometry.getLineStrings();
for(var i=0;i<lineStringsArray.length;i++){

  lineStringsArray[i].forEachSegment(function(start, end) {
    var dx = end[0] - start[0];
    var dy = end[1] - start[1];
    var rotation = Math.atan2(dy, dx);
    styles.push(new ol.style.Style({
      geometry: new ol.geom.Point(end),
      image: new ol.style.Icon({
        src: 'https://openlayers.org/en/v3.19.1/examples/data/arrow.png',
        rotateWithView: false,
        rotation: -rotation
      })
    }));
  });
}

  return styles;
};

 

### 使用 OpenLayers 实现箭头线 要在 OpenLayers 中实现箭头线,可以通过自定义样式来完成。以下是详细的说明以及代码示例。 #### 创建矢量图层和数据源 首先需要创建一个 `VectorSource` 和一个 `VectorLayer` 来存储几何对象并将其渲染到地图上[^2]。这些几何对象可以是点、线或多边形,在此场景下我们将使用 `LineString` 表示一条直线。 ```javascript import { Map, View } from &#39;ol&#39;; import TileLayer from &#39;ol/layer/Tile&#39;; import OSM from &#39;ol/source/OSM&#39;; import VectorLayer from &#39;ol/layer/Vector&#39;; import VectorSource from &#39;ol/source/Vector&#39;; import LineString from &#39;ol/geom/LineString&#39;; import Feature from &#39;ol/Feature&#39;; ``` #### 定义箭头样式的函数 为了使线条末端显示箭头效果,需通过 `StyleFunction` 动态计算箭头的方向和位置。这通常涉及三角学中的角度计算[^2]。 ```javascript function createArrowStyle(lineGeometry) { const lastSegment = lineGeometry.getCoordinates().slice(-2); if (lastSegment.length !== 2) return null; const p1 = lastSegment[0]; const p2 = lastSegment[1]; // 计算箭头方向的角度 const dx = p2[0] - p1[0]; const dy = p2[1] - p1[1]; const rotation = Math.atan2(dy, dx); return new Style({ image: new Icon({ src: &#39;arrow.png&#39;, // 替换为实际箭头图片路径 anchor: [0.75, 0.5], rotateWithView: true, rotation: rotation, }), geometry: function () { return new Point(p2); // 将图标放置在线条终点处 }, }); } ``` #### 添加样式至特征 将上述箭头样式应用到具体的 `Feature` 上,并设置默认线条样式作为补充[^2]。 ```javascript const vectorSource = new VectorSource(); // 构建折线几何体 const lineGeom = new LineString([ [0, 0], // 起始坐标 [1000000, 1000000], // 结束坐标 ]); // 应用样式给特性 const feature = new Feature({ geometry: lineGeom }); feature.setStyle((feature, resolution) => [ new Style({ stroke: new Stroke({ color: &#39;#ffcc33&#39;, width: 3, }), }), createArrowStyle(lineGeom), ]); vectorSource.addFeature(feature); // 渲染矢量图层 const vectorLayer = new VectorLayer({ source: vectorSource }); new Map({ target: &#39;map-container&#39;, layers: [new TileLayer({ source: new OSM() }), vectorLayer], view: new View({ center: [0, 0], zoom: 4, }), }); ``` 以上代码展示了如何利用 OpenLayers 的 API 配合自定义样式绘制箭头线路[^2]。 ---
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值