openlayers 3 : draw arrow on multi line

2019-09-07 20:43发布

I'd like to know if it's possible to draw an arrow icon on a MultiLineString

My aim is to show multiline with an arrow on every line of the multiLine.

I have seen some examples on the web, but those are always with a single line.

Do you know if it's possible to do it with a multiLineString?

1条回答
一纸荒年 Trace。
2楼-- · 2019-09-07 21:26

Yes, it is possible.

  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;
};

Have a look at demo Link https://plnkr.co/edit/UM7bD8Pq55PjWQ6EHmTl?p=preview

查看更多
登录 后发表回答