【转】openlayers-方向标注

admin【转】openlayers-方向标注已关闭评论条评论 1,332 次浏览






版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/staHuri/article/details/88867365



openlayers-方向标注

需求

  • 给LineString 标注方向
  • 效果图1553751736094

设计思路

  1. 设计一个方向箭头图标
  2. 计算线段方向(LineString 可以放很多坐标而线段方向是两点之间)
    1. (0,1)(1,2)(2,3)…方式计算每一段的方向角度
  3. 方向点安放坐标
    1. let array_coor = lineStringForl.getCoordinateAt(i * 1.0 / res);
      
      
      
      
      
      
      
      
      
      
      • 1

主要函数

  • 单一线段计算角度
    function calcD(start, end) {
        let dx = end[0] - start[0];
        let dy = end[1] - start[1];
        let rotation = Math.atan2(dy, dx);
        console.log("角度" + rotation);
        return rotation;
      }
    
    
    
    
    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 从坐标组中计算线段以及每一段的方向角
    // 计算线段角度
    function calcLineStringD(coordis) {
      let n = coordis.length;
      let vs = [];
      for (let i = 0; i < n; i++) {
        if (i + 1 < n) {
          vs.push({
            "line": [coordis[i], coordis[i + 1]],
            "d": calcD(coordis[i], coordis[i + 1])
          })
        }
      }
      return vs;
    }
    
    
    
    
    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 设置样式
    new ol.style.Style({
      geometry: new ol.geom.Point(array_coor),
      image: new ol.style.Icon({
        src: 'http://localhost:9999/titles/fx.png',
        anchor: [0.75, 0.5],
        rotateWithView: true,
        rotation: -rat
      })
    })
    
    
    
    
    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

完整demo

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>方向箭头</title>
  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
          integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
          crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js"
          integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4"
          crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js"
          integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1"
          crossorigin="anonymous"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/openlayers/4.6.5/ol.css"
        integrity="sha256-rQq4Fxpq3LlPQ8yP11i6Z2lAo82b6ACDgd35CKyNEBw=" crossorigin="anonymous"/>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/openlayers/4.6.5/ol.js"
          integrity="sha256-77IKwU93jwIX7zmgEBfYGHcmeO0Fx2MoWB/ooh9QkBA="
          crossorigin="anonymous"></script>

  <script src="https://unpkg.com/rbush@2.0.1/rbush.min.js"></script>


</head>
<body>
<div id="map"></div>
<script>
  var wkt = 'LineString( 120 30 , 121 31 ,121 32 ,122 33 )';

  var format = new ol.format.WKT();

  var feature = format.readFeature(wkt, {});

  // 将方向标注放在线段中间
  function direction_labeling_02(res, lineStringForl, coordis, vs, styles) {
    for (let i = 0; i < vs.length; i++) {
      let line = vs[i]['line'];
      let start =line[0];
      let end = line[1];
      let array_coor = [
        (start[0]+end[0])/2, (start[1]+end[1])/2
      ];
      let d = vs[i]['d'];

      styles.push(new ol.style.Style({
        geometry: new ol.geom.Point(array_coor),
        image: new ol.style.Icon({
          src: 'http://localhost:9999/titles/fx.png',
          anchor: [0.75, 0.5],
          rotateWithView: true,
          rotation: -d
        })
      }))
    }


  }


  function direction_labeling_01(res, lineStringForl, coordis, vs, styles) {
// 这个地方可以修改成你要的箭头数量
    let arrow_count = res * 1.0 * 40;

    for (let i = 0; i < arrow_count; i++) {
      // 求坐标
      let array_coor = lineStringForl.getCoordinateAt(i * 1.0 / res);

      // // array_coor 在 coordis 不显示
      if (inc(coordis, array_coor)) {
        console.log("相同" + i);
        continue;
      }
      let rat = calcLinePointD(vs, array_coor);

      // 根据 array_coor 在那一条线段上设置 旋转角度
      styles.push(new ol.style.Style({
        geometry: new ol.geom.Point(array_coor),
        image: new ol.style.Icon({
          src: 'http://localhost:9999/titles/fx.png',
          anchor: [0.75, 0.5],
          rotateWithView: true,
          rotation: -rat
        })
      }));
    }
  }

  /**
   *
   * @param feature feature
   * @param res 总共多少个箭头
   * @returns {ol.style.Style[]}
   */
  function styleFunction(feature, res) {
    var lineStringForl = feature.getGeometry();

    var coordis = lineStringForl.getCoordinates();

    var styles = [
      new ol.style.Style({
        stroke: new ol.style.Stroke({
          color: '#23218b',
          width: 10
        })
      })
    ];

    // 计算线段角度
    let vs = calcLineStringD(coordis);
    // direction_labeling_01(res, lineStringForl, coordis, vs, styles);
    direction_labeling_02(res, lineStringForl, coordis, vs, styles);

    return styles;
  }

  // 计算点是否在线上
  function pointInLine(line, Q) {

    let pi = line[0];
    let pj = line[1];

    if ((Q[0] - pi[0]) * (pj[1] - pi[1]) == (pj[0] - pi[0]) * (Q[1] - pi[1]) && Math.min(pi[0], pj[0])
        <= Q[0] && Q[0] <= Math.max(pi[0], pj[0]) && Math.min(pi[1], pj[1]) <= Q[1] && Q[1] <= Math.max(pi[1],
            pj[1])) {
      console.log("再线段上");
      return true;
    } else {
      console.log("不再线段上");
      return false;
    }

  }

  // 返回点所在线段的具体角度
  function calcLinePointD(vs, point) {
    for (let i = 0; i < vs.length; i++) {
      let line = vs[i]['line'];
      let d = vs[i]['d'];
      if (pointInLine(line, point) == true) {
        return d;
      }
    }
  }

  // 计算线段角度
  function calcLineStringD(coordis) {
    let n = coordis.length;
    let vs = [];
    for (let i = 0; i < n; i++) {
      if (i + 1 < n) {
        vs.push({
          "line": [coordis[i], coordis[i + 1]],
          "d": calcD(coordis[i], coordis[i + 1])
        })
      }
    }
    return vs;
  }

  // 计算线段角度
  function calcD(start, end) {
    let dx = end[0] - start[0];
    let dy = end[1] - start[1];
    let rotation = Math.atan2(dy, dx);
    console.log("角度" + rotation);
    return rotation;
  }

  /**
   * 判断坐标组合cood中是否有atc,
   * 不会判断强写
   * @param cood 坐标组合
   * @param atc 坐标
   * @returns {boolean}
   */
  function inc(cood, atc) {
    for (c in cood) {
      if (cood[c][0] == atc[0] && cood[c][1] == atc[1]) {
        return true;
      }
    }
    return false;
  }

  var vector = new ol.layer.Vector({
    source: new ol.source.Vector({
      features: [feature]
    }),
    style: styleFunction(feature, 100)
  });
  var gaodeMapLayer = new ol.layer.Tile({
    source: new ol.source.XYZ({
      url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}'
    })
  });

  var map = new ol.Map({
    layers: [gaodeMapLayer, vector],
    view: new ol.View({
      center: [120, 30],
      projection: 'EPSG:4326',
      zoom: 5
    }),
    target: 'map'
  });
</script>
</body>
</html>










  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210