• vue + openlayer 按路径移动


    示例

    创建一个方形的规矩,并让点按轨迹移动。效果如下:
    在这里插入图片描述

    源代码

    <template>
      <div>
        <div id="map" class="map"></div>
        <button id="start-animation" ref="startButton">Start Animation</button>
      </div>
    </template>
    
    <script>
    import "ol/ol.css";
    import "@/assets/css/map.css";
    import { Map, View, Feature } from "ol";
    import { Circle as CircleStyle, Fill, Icon, Stroke, Style } from "ol/style";
    import { OSM, Vector as VectorSource } from "ol/source";
    import { Tile as TileLayer, Vector as VectorLayer } from "ol/layer";
    import { getVectorContext } from "ol/render";
    import { LineString, Point } from "ol/geom";
    import { ref } from "vue";
    
    export default {
      setup(props) {
        const map = ref(null);
        const startButton = ref(null);
        const styles = {
          route: new Style({
            stroke: new Stroke({
              width: 6,
              color: [237, 212, 0, 0.8],
            }),
          }),
          icon: new Style({
            image: new Icon({
              anchor: [0.5, 1],
              src: "https://openlayers.org/en/latest/examples/data/icon.png",
            }),
          }),
          geoMarker: new Style({
            image: new CircleStyle({
              radius: 7,
              fill: new Fill({ color: "red" }),
              stroke: new Stroke({
                color: "white",
                width: 2,
              }),
            }),
          }),
        };
    
        return { map, styles, startButton };
      },
    
      data() {
        return {
          speedInput: 5,
          animating: false,
          distance: 0,
          lastTime: null,
          position: null,
          geoMarker: null,
          vectorLayer: null,
          route: null,
        };
      },
    
      mounted() {
        this.map = new Map({
          layers: [
            new TileLayer({
              source: new OSM(),
            }),
          ],
          target: "map",
          view: new View({
            center: [-5645116.561407479, -3504865.8960142885],
            zoom: 10,
          }),
        });
    
        this.createRoute();
    
        var that = this;
        that.startButton.addEventListener("click", function () {
          if (that.animating) {
            that.stopAnimation();
          } else {
            that.startAnimation();
          }
        });
      },
    
      methods: {
        moveFeature(event) {
          const speed = 20;
          const time = event.frameState.time;
          const elapsedTime = time - this.lastTime;
          this.distance = (this.distance + (speed * elapsedTime) / 1e5) % 2;
          this.lastTime = time;
    
          //按比例获取坐标位置
          const currentCoordinate = this.route.getCoordinateAt(
            this.distance > 1 ? 2 - this.distance : this.distance
          );
          this.position.setCoordinates(currentCoordinate);
          const vectorContext = getVectorContext(event);
          vectorContext.setStyle(this.styles.geoMarker);
          vectorContext.drawGeometry(this.position);
          // tell OpenLayers to continue the postrender animation
          this.map.render();
        },
    
        startAnimation() {
          this.animating = true;
          this.lastTime = Date.now();
          this.startButton.textContent = "Stop Animation";
          this.vectorLayer.on("postrender", this.moveFeature);
          // hide geoMarker and trigger map render through change event
          this.geoMarker.setGeometry(null);
        },
    
        stopAnimation() {
          this.animating = false;
          this.startButton.textContent = "Start Animation";
    
          // Keep marker at current animation position
          this.geoMarker.setGeometry(this.position);
          this.vectorLayer.un("postrender", this.moveFeature);
        },
    
        createRoute() {
          var that = this;
    
          var coordinates = [
            [-5701523.274225562, -3508003.9130105707],
            [-5570600.171389932, -3508003.9130105707],
            [-5570600.171389932, -3522590.9336281433],
            [-5701523.274225562, -3522590.9336281433],
            [-5701523.274225562, -3508003.9130105707],
          ];
    
          that.route = new LineString(coordinates);
    
          const routeFeature = new Feature({
            type: "route",
            geometry: that.route,
          });
    
          const startMarker = new Feature({
            type: "icon",
            geometry: new Point(that.route.getFirstCoordinate()),
          });
          const endMarker = new Feature({
            type: "icon",
            geometry: new Point(that.route.getLastCoordinate()),
          });
    
          that.position = startMarker.getGeometry().clone();
          that.geoMarker = new Feature({
            type: "geoMarker",
            geometry: that.position,
          });
    
          that.vectorLayer = new VectorLayer({
            source: new VectorSource({
              features: [routeFeature, that.geoMarker, startMarker, endMarker],
            }),
            style: function (feature) {
              return that.styles[feature.get("type")];
            },
          });
    
          that.map.addLayer(that.vectorLayer);
        },
      },
    };
    </script>
    
    
    
    • 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
  • 相关阅读:
    百度Mysql面试题总结
    ARMv7/ARMv8/ARMv9架构你不知道的那些事
    【C语言必知必会| 第十篇】指针入门,这一篇就够了
    【iOS】—— RunLoop详解
    【三种修改 list 的方法测试】
    如何从base64中获取图像的宽度、高度、Uint8ClampedArray
    请求报错:javax.net.ssl.SSLHandshakeException: No appropriate protocol
    【TrueType】Converting Outlines to the TrueType Format(将轮廓转换为TrueType格式)
    pytorch 实现线性回归(Pytorch 03)
    Leetcode13. 罗马数字转整数
  • 原文地址:https://blog.csdn.net/weixin_39340061/article/details/133344537