openlayers轨迹回放Demo

<!DOCTYPE html>
<html>
  <head>
    <title>openlayers轨迹回放</title>
    <link
      rel="stylesheet"
      href="https://openlayers.org/en/v6.5.0/css/ol.css"
      type="text/css"
    />
    <script src="https://openlayers.org/en/v6.5.0/build/ol.js"></script>
    <style>
      html,
      body,
      #map {
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
  </body>
  <script>
    //初始化地图
    var map = new ol.Map({
      target: "map",
      layers: [
        new ol.layer.Tile({
          source: new ol.source.XYZ({
            url: "http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}",
          }),
        }),
      ],
      view: new ol.View({
        center: [120.86129891934904, 30.876200859534276],
        projection: "EPSG:4326",
        zoom: 13.4,
      }),
    });

    function addlinestring(routeCoords) {
      //添加线
      route = new ol.geom.LineString(routeCoords);
      routeFeature = new ol.Feature({
        type: "route",
        geometry: route,
      });
      //添加起点
      let startMarker = new ol.Feature({
        type: "icon",
        geometry: new ol.geom.Point(route.getFirstCoordinate()),
      });
      //添加终点
      let endMarker = new ol.Feature({
        type: "icon",
        geometry: new ol.geom.Point(route.getLastCoordinate()),
      });
      //绘制移动的点
      position = startMarker.getGeometry().clone();
      geoMarker = new ol.Feature({
        type: "geoMarker",
        geometry: position,
      });
      styles = {
        route: new ol.style.Style({
          stroke: new ol.style.Stroke({
            width: 6,
            color: [237, 212, 0, 1],
          }),
        }),
        icon: new ol.style.Style({
          image: new ol.style.Circle({
            radius: 7,
            fill: new ol.style.Fill({ color: [0, 0, 255, 1] }),
          }),
        }),
        geoMarker: new ol.style.Style({
          image: new ol.style.Circle({
            radius: 7,
            fill: new ol.style.Fill({ color: [0, 255, 0, 1] }),
          }),
        }),
      };
      layerss = new ol.layer.Vector({
        source: new ol.source.Vector({
          features: [routeFeature, geoMarker, startMarker, endMarker],
        }),
        style: function (feature) {
          return styles[feature.get("type")];
        },
      });
      map.addLayer(layerss);
      startAnimation();
    }
    let animating = false;
    let distance = 0;
    let lastTime;
    function moveFeature(event) {
      let speed = 50; //移动速度
      let time = event.frameState.time;
      let elapsedTime = time - lastTime;
      distance = (distance + (speed * elapsedTime) / 1e6) % 2;
      lastTime = time;
      let currentCoordinate = route.getCoordinateAt(
        distance > 1 ? 2 - distance : distance
      );
      position.setCoordinates(currentCoordinate);
    }
    //开始移动
    function startAnimation() {
      animating = true;
      lastTime = Date.now();
      layerss.on("postrender", moveFeature);
      // geoMarker.setGeometry(null);
    }
    //停止移动
    function stopAnimation() {
      animating = true;
      geoMarker.setGeometry(position);
      layerss.un("postrender", moveFeature);
    }
    //轨迹线
    let routeCoords = [
      [120.83420068030844, 30.834708668385822],
      [120.83600312476644, 30.8362536207784],
      [120.83701163535603, 30.83679006258138],
      [120.83965092902669, 30.838184811269123],
      [120.84926362981518, 30.84256338366904],
      [120.85012544864018, 30.84381214147104],
      [120.85408099742031, 30.851182880348983],
      [120.86836725117884, 30.87103376118821],
      [120.87796437514476, 30.879769103042214],
      [120.8805888935645, 30.881766871853934],
      [120.8655860429288, 30.901078635664142],
      [120.86059202871014, 30.90893711390783],
      [120.86806024700445, 30.912217546379573],
    ];

    addlinestring(routeCoords);
  </script>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值