当前位置:   article > 正文

Vue+Openlayer实现轨迹路线_openlayers轨迹绘制

openlayers轨迹绘制

  1. <template>
  2. <div>
  3. <div id="map" ref="map" style="width: 100vw; height: 100vh"></div>
  4. </div>
  5. </template>
  6. <script>
  7. import VectorLayer from "ol/layer/Vector";
  8. import VectorSource from "ol/source/Vector";
  9. import Feature from "ol/Feature";
  10. import { Draw } from "ol/interaction";
  11. import { Style, Fill, Stroke, Circle, Icon } from "ol/style";
  12. import { transform, fromLonLat, toLonLat } from "ol/proj";
  13. import { Point, LineString } from "ol/geom";
  14. import { Map, View, interaction, events } from "ol";
  15. import TileLayer from "ol/layer/Tile";
  16. import { defaults as defaultControls } from "ol/control";
  17. import XYZ from "ol/source/XYZ";
  18. export default {
  19. data() {
  20. return {
  21. map: {},
  22. featureMove: {},
  23. carPoints: [], //车还要走的点
  24. routeIndex: 0, //当前小车所在的路段
  25. timer: {},
  26. coordinates: [
  27. [10836932.628965743, 4998172.218425438],
  28. [10638182.82599503, 3781582.515392581],
  29. [10897159.841987172, 3552719.105911153],
  30. [11120000.530166456, 4986126.775821152],
  31. [11360909.382252172, 4895785.956289009],
  32. [11053750.595842887, 3420219.23726401],
  33. [11294659.4479286, 3257605.7621061527],
  34. [11565681.906525029, 4823513.300663294],
  35. [11866817.971632171, 4757263.366339724],
  36. [11535568.300014313, 3185333.1064804387],
  37. [11812613.479912885, 3058855.959135439],
  38. [12125794.987624314, 4721127.038526867],
  39. [12402840.167522885, 4684990.710714009],
  40. [12023408.725487886, 2926356.090488296],
  41. [12300453.905386457, 2860106.1561647244],
  42. [12643749.0196086, 4630786.218994724],
  43. [12866589.707787886, 4510331.792951867],
  44. [12547385.478774315, 2878174.3200711533],
  45. [12932839.642111458, 2878174.3200711533],
  46. [13113521.281175744, 3751468.908881867],
  47. [13125566.723780029, 4739195.202433295],
  48. [13691702.526181456, 5425785.43087758],
  49. [13553179.936232172, 6112375.659321865],
  50. [12920794.199507171, 5407717.266971151],
  51. [12065567.774602886, 4974081.3332168665],
  52. [12788294.330860028, 4895785.956289009],
  53. ],
  54. routeLayer: {},
  55. };
  56. },
  57. mounted() {
  58. this.initMap(); //初始化地图方法
  59. this.open(); //自动开启功能
  60. },
  61. methods: {
  62. //初始化地图
  63. initMap() {
  64. this.map = new Map({
  65. target: "map",
  66. layers: [
  67. new TileLayer({
  68. source: new XYZ({
  69. url: "http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}",
  70. }),
  71. }),
  72. ],
  73. view: new View({
  74. center: fromLonLat([108.522097, 37.272848]),
  75. zoom: 4.7,
  76. }),
  77. });
  78. },
  79. //添加矢量图层
  80. async open() {
  81. //画轨迹线
  82. await this.drawLine();
  83. //开始动
  84. this.moveStart();
  85. },
  86. //轨迹线 把每个点连起来
  87. drawLine() {
  88. this.routeLayer = new VectorLayer({
  89. source: new VectorSource({
  90. features: [],
  91. }),
  92. });
  93. this.map.addLayer(this.routeLayer);
  94. let comDots = [];
  95. let wireFeature = {};
  96. this.coordinates.forEach((item) => {
  97. comDots.push(item);
  98. wireFeature = new Feature({
  99. geometry: new LineString(comDots),
  100. });
  101. wireFeature.setStyle(
  102. new Style({
  103. stroke: new Stroke({
  104. // 设置边的样式
  105. color: "rgb(21, 106, 158)",
  106. width: 3,
  107. }),
  108. })
  109. );
  110. this.routeLayer.getSource().addFeatures([wireFeature]);
  111. });
  112. },
  113. //创建小车这个要素
  114. moveStart() {
  115. //坐标转换
  116. this.dotsData = this.coordinates.map((item) => {
  117. return transform(item, "EPSG:3857", "EPSG:4326");
  118. });
  119. //深复制车的位置,不在原数组改变,方便重新播放
  120. // this.carPoints = JSON.parse(JSON.stringify(this.dotsData));
  121. this.carPoints = [...this.dotsData];
  122. //小车最初位置在第一个坐标点
  123. this.featureMove = new Feature({
  124. geometry: new Point(this.carPoints[0]),
  125. });
  126. this.featureMove.setStyle(
  127. new Style({
  128. image: new Icon({
  129. src: "https://openlayers.org/en/v4.6.5/examples/data/icon.png",
  130. scale: 0.85,
  131. anchor: [0.5, 0.5],
  132. rotation: this.countRotate(),
  133. }),
  134. })
  135. );
  136. //添加车辆元素到图层
  137. this.routeLayer.getSource().addFeature(this.featureMove);
  138. this.timeStart();
  139. },
  140. //计时器开始
  141. timeStart() {
  142. this.timer = setInterval(() => {
  143. if (this.routeIndex + 1 >= this.carPoints.length) {
  144. //重头开始
  145. this.routeIndex = 0;
  146. //移除要素
  147. this.routeLayer.getSource().removeFeature(this.featureMove);
  148. clearInterval(this.timer);
  149. //重复运动
  150. this.open(); //自动开启功能
  151. return;
  152. }
  153. //到转折点旋转角度
  154. if (this.nextPoint() === this.carPoints[this.routeIndex + 1]) {
  155. this.routeIndex++;
  156. this.featureMove
  157. .getStyle()
  158. .getImage()
  159. .setRotation(this.countRotate());
  160. }
  161. //改变坐标点
  162. this.featureMove
  163. .getGeometry()
  164. .setCoordinates(fromLonLat(this.carPoints[this.routeIndex]));
  165. }, 10);
  166. },
  167. //计算下一个点的位置
  168. //这里的算法是计算了两点之间的点 两点之间的连线可能存在很多个计算出来的点
  169. nextPoint() {
  170. let routeIndex = this.routeIndex;
  171. let p1 = this.map.getPixelFromCoordinate(
  172. fromLonLat(this.carPoints[routeIndex])
  173. ); //获取在屏幕的像素位置
  174. let p2 = this.map.getPixelFromCoordinate(
  175. fromLonLat(this.carPoints[routeIndex + 1])
  176. );
  177. let dx = p2[0] - p1[0];
  178. let dy = p2[1] - p1[1];
  179. //打印可见 在没有走到下一个点之前,下一个点是不变的,前一个点以这个点为终点向其靠近
  180. let distance = Math.sqrt(dx * dx + dy * dy);
  181. if (distance <= 1) {
  182. return this.carPoints[routeIndex + 1];
  183. } else {
  184. let x = p1[0] + dx / distance;
  185. let y = p1[1] + dy / distance;
  186. let coor = transform(
  187. this.map.getCoordinateFromPixel([x, y]),
  188. "EPSG:3857",
  189. "EPSG:4326"
  190. );
  191. this.carPoints[routeIndex] = coor; //这里会将前一个点重新赋值 要素利用这个坐标变化进行移动
  192. return this.carPoints[routeIndex];
  193. }
  194. },
  195. //计算两点之间的角度 算旋转角度
  196. countRotate() {
  197. let i = this.routeIndex,
  198. j = i + 1;
  199. if (j === this.carPoints.length) {
  200. i--;
  201. j--;
  202. }
  203. let p1 = this.carPoints[i];
  204. let p2 = this.carPoints[j];
  205. return Math.atan2(p2[0] - p1[0], p2[1] - p1[1]);
  206. },
  207. },
  208. };
  209. </script>
  210. <style lang="scss" scoped>
  211. </style>

 

  1. <template>
  2. <div>
  3. <div id="map" style="width: 100vw; height: 100vh" />
  4. <div id="geo-marker" class="css_animation">
  5. <img :src="myplanImg" />
  6. </div>
  7. <div class="measuretip" id="speed"></div>
  8. </div>
  9. </template>
  10. <script>
  11. import "ol/ol.css";
  12. import { Map, View, Feature, Overlay } from "ol";
  13. import { Vector as VectorLayer } from "ol/layer";
  14. import { OSM, Vector as VectorSource } from "ol/source";
  15. import { Point, LineString } from "ol/geom.js";
  16. import { Icon, Fill, Stroke, Style, Circle } from "ol/style";
  17. import TileLayer from "ol/layer/Tile";
  18. export default {
  19. data() {
  20. return {
  21. path: [
  22. [115.62, 14.82],
  23. [112.79, 14.82],
  24. [114.6636, 18.2977],
  25. [111.687, 18.897],
  26. [110.3014, 15.063],
  27. ], // 模拟路径
  28. pathIndex: 0, // 路径点索引
  29. marker: {}, //移动点
  30. splitNumber: 200, // 每两个经纬度之间的分割点
  31. setIntervalTime: 30, // 移动点间隔时间
  32. myplanImg: "https://openlayers.org/en/v4.6.5/examples/data/icon.png", // 移动点的图片
  33. helpTooltipElement: {}, // 平台信息div
  34. helpTooltip: {}, // 平台信息overlay
  35. sourceFeatures: new VectorSource(),
  36. lineString: new LineString([]),
  37. };
  38. },
  39. created() {
  40. this.analysisPath(this.splitNumber);
  41. },
  42. mounted() {
  43. this.initSeamap();
  44. },
  45. methods: {
  46. initSeamap: function () {
  47. this.pathIndex = this.path.length - 1;
  48. this.map = new Map({
  49. target: "map",
  50. view: new View({
  51. projection: "EPSG:4326",
  52. center: [109.8, 18.4],
  53. zoom: 7,
  54. minZoom: 3, // 限制最大显示
  55. maxZoom: 14,
  56. }),
  57. layers: [
  58. new TileLayer({
  59. source: new OSM(),
  60. }),
  61. new VectorLayer({
  62. // 两点之间的连线
  63. source: new VectorSource({
  64. features: [
  65. new Feature({
  66. geometry: this.lineString,
  67. }),
  68. ],
  69. }),
  70. style: [
  71. new Style({
  72. stroke: new Stroke({
  73. width: 3,
  74. color: "rgba(0, 0, 0, 1)",
  75. lineDash: [0.1, 5],
  76. }),
  77. zIndex: 2,
  78. }),
  79. ],
  80. updateWhileAnimating: true,
  81. }),
  82. new VectorLayer({
  83. // 两端点Feature
  84. source: this.sourceFeatures,
  85. }),
  86. ],
  87. });
  88. this.marker = new Overlay({
  89. positioning: "center-center",
  90. offset: [0, 0],
  91. element: document.getElementById("geo-marker"),
  92. stopEvent: false,
  93. });
  94. this.map.addOverlay(this.marker);
  95. let style1 = [
  96. // 开始结束点样式
  97. new Style({
  98. image: new Icon({
  99. src: "https://openlayers.org/en/v4.6.5/examples/data/icon.png",
  100. }),
  101. }),
  102. ];
  103. let feature_start = new Feature({
  104. geometry: new Point(this.path[0]),
  105. });
  106. let feature_end = new Feature({
  107. geometry: new Point(this.path[this.path.length - 1]),
  108. });
  109. feature_start.setStyle(style1);
  110. feature_end.setStyle(style1);
  111. this.sourceFeatures.addFeatures([feature_start, feature_end]);
  112. this.lineString.setCoordinates(this.path);
  113. this.helpTooltipElement = document.createElement("div");
  114. this.helpTooltipElement.className = "measuretip";
  115. this.helpTooltipElement.id = "speed";
  116. this.helpTooltip = new Overlay({
  117. element: this.helpTooltipElement,
  118. offset: [15, 0],
  119. positioning: "center-left",
  120. });
  121. this.map.addOverlay(this.helpTooltip);
  122. this.map.once("postcompose", (event) => {
  123. setInterval(() => {
  124. this.animation();
  125. }, this.setIntervalTime);
  126. });
  127. },
  128. animation: function () {
  129. if (this.pathIndex === -1) {
  130. this.pathIndex = this.path.length - 1;
  131. }
  132. this.marker.setPosition(this.path[this.pathIndex]);
  133. this.helpTooltipElement.innerHTML =
  134. "<B>名称:</B>船载应急通信系统" +
  135. "<br>" +
  136. "<B>子系统:</B>平台A,平台B" +
  137. "<br>" +
  138. "<B>经纬度:</B>" +
  139. (this.path[this.pathIndex][0] + "").substring(0, 6) +
  140. "," +
  141. (this.path[this.pathIndex][1] + "").substring(0, 5);
  142. this.helpTooltip.setPosition(this.path[this.pathIndex]);
  143. this.pathIndex--;
  144. },
  145. analysisPath: function (splitNumber) {
  146. let tempPath = [...this.path];
  147. let pathResults = [];
  148. let tempPoint = [0, 0];
  149. if (tempPath.length > 1) {
  150. for (let i = 0; i < tempPath.length - 1; i++) {
  151. // 每两个点之间分割出splitNumber个点
  152. pathResults.push(tempPath[i]);
  153. for (let j = 0; j < splitNumber; j++) {
  154. tempPoint[0] =
  155. ((tempPath[i + 1][0] - tempPath[i][0]) * (j + 1)) / splitNumber +
  156. tempPath[i][0];
  157. tempPoint[1] =
  158. ((tempPath[i + 1][1] - tempPath[i][1]) * (j + 1)) / splitNumber +
  159. tempPath[i][1];
  160. pathResults.push([...tempPoint]);
  161. }
  162. }
  163. pathResults.push(tempPath[tempPath.length - 1]);
  164. this.path = [...pathResults];
  165. }
  166. },
  167. },
  168. };
  169. </script>
  170. <style>
  171. .measuretip {
  172. position: relative;
  173. background-color: #0d9bf2;
  174. opacity: 0.7;
  175. border-radius: 3px;
  176. padding: 10px;
  177. font-size: 12px;
  178. cursor: default;
  179. }
  180. </style>

  1. <template>
  2. <div>
  3. <div id="map" style="width: 100vw; height: 100vh" />
  4. <div style="position: fixed; top: 200px; left: 200px">
  5. <el-button @click="startAnimation()">开始</el-button>
  6. <el-button @click="stopAnimation()">暂停</el-button>
  7. </div>
  8. </div>
  9. </template>
  10. <script>
  11. import "ol/ol.css";
  12. import { Map, View, Feature, Overlay } from "ol";
  13. import { Vector as VectorLayer } from "ol/layer";
  14. import { XYZ, OSM, Vector as VectorSource } from "ol/source";
  15. import { Point, LineString } from "ol/geom.js";
  16. import { Icon, Fill, Stroke, Style, Circle } from "ol/style";
  17. import TileLayer from "ol/layer/Tile";
  18. import { ScaleLine, defaults as defaultControls } from "ol/control";
  19. import { getVectorContext } from "ol/render";
  20. export default {
  21. data() {
  22. return {
  23. map: {},
  24. route: new LineString([
  25. [115.62, 14.82],
  26. [112.79, 14.82],
  27. [114.6636, 18.2977],
  28. [111.687, 18.897],
  29. [110.3014, 15.063],
  30. ]),
  31. geometryMove: {},
  32. featureMove: {},
  33. styles: {
  34. route: new Style({
  35. stroke: new Stroke({
  36. width: 6,
  37. color: [237, 212, 0, 0.8],
  38. }),
  39. }),
  40. icon: new Style({
  41. image: new Icon({
  42. anchor: [0.5, 1],
  43. src: "https://openlayers.org/en/v4.6.5/examples/data/icon.png",
  44. scale: 1, //设置大小
  45. }),
  46. }),
  47. featureMove: new Style({
  48. image: new Circle({
  49. radius: 7,
  50. fill: new Fill({ color: "black" }),
  51. stroke: new Stroke({
  52. color: "white",
  53. width: 2,
  54. }),
  55. }),
  56. }),
  57. },
  58. vectorLayer: {},
  59. distance: 0,
  60. lastTime: 0,
  61. speed: 0.1,
  62. };
  63. },
  64. created() {},
  65. mounted() {
  66. this.initMap();
  67. },
  68. methods: {
  69. initMap() {
  70. this.geometryMove = new Point(this.route.getFirstCoordinate());
  71. this.featureMove = new Feature({
  72. type: "featureMove",
  73. geometry: this.geometryMove,
  74. });
  75. this.vectorLayer = new VectorLayer({
  76. source: new VectorSource({
  77. features: [
  78. new Feature({
  79. type: "route",
  80. geometry: this.route,
  81. }),
  82. this.featureMove,
  83. new Feature({
  84. type: "icon",
  85. geometry: new Point(this.route.getFirstCoordinate()),
  86. }),
  87. new Feature({
  88. type: "icon",
  89. geometry: new Point(this.route.getLastCoordinate()),
  90. }),
  91. ],
  92. }),
  93. style: (feature) => {
  94. return this.styles[feature.get("type")];
  95. },
  96. });
  97. this.map = new Map({
  98. target: "map",
  99. view: new View({
  100. projection: "EPSG:4326",
  101. center: [109.8, 18.4],
  102. zoom: 7,
  103. }),
  104. layers: [
  105. new TileLayer({
  106. source: new OSM(),
  107. }),
  108. ],
  109. });
  110. this.map.addLayer(this.vectorLayer);
  111. },
  112. moveFeature(e) {
  113. let time = e.frameState.time;
  114. this.distance =
  115. (this.distance + (this.speed * (time - this.lastTime)) / 1000) % 1; //%2表示:起止止起;%1表示:起止起止
  116. this.lastTime = time;
  117. const currentCoordinate = this.route.getCoordinateAt(
  118. this.distance > 1 ? 2 - this.distance : this.distance
  119. );
  120. this.geometryMove.setCoordinates(currentCoordinate);
  121. const vectorContext = getVectorContext(e);
  122. vectorContext.setStyle(this.styles.featureMove);
  123. vectorContext.drawGeometry(this.geometryMove);
  124. this.map.render();
  125. },
  126. startAnimation() {
  127. this.lastTime = Date.now();
  128. this.vectorLayer.on("postrender", this.moveFeature);
  129. this.featureMove.setGeometry(null); //必须用null,不能用{}
  130. },
  131. stopAnimation() {
  132. this.featureMove.setGeometry(this.geometryMove);
  133. this.vectorLayer.un("postrender", this.moveFeature);
  134. },
  135. },
  136. };
  137. </script>

使用:

  1. <template>
  2. <div>
  3. <div id="map" style="height: 100vh; width: 100vw"></div>
  4. <div style="position: fixed; top: 100px; left: 200px">
  5. <el-button @click="openAnimation()">打开动画</el-button>
  6. <el-button @click="closeAnimation()">关闭动画</el-button>
  7. </div>
  8. <LjOlCarRoute
  9. ref="ref_LjOlCarRoute"
  10. :map="map"
  11. :coordinates="coordinates_route"
  12. :speed="0.1"
  13. v-if="showAnimation"
  14. ></LjOlCarRoute>
  15. </div>
  16. </template>
  17. <script>
  18. import "ol/ol.css";
  19. import { Map, View } from "ol";
  20. import { OSM } from "ol/source";
  21. import { Tile as TileLayer } from "ol/layer";
  22. import LjOlCarRoute from "@/components/LjOlCarRoute/index.vue";
  23. export default {
  24. components: { LjOlCarRoute },
  25. data() {
  26. return {
  27. map: {},
  28. showAnimation: false,
  29. coordinates_route: [
  30. [104.2979180563, 30.528298024],
  31. [104.2987389704, 30.527798338],
  32. [104.2974397847, 30.5265062907],
  33. [104.296943667091881, 30.52468992916009],
  34. ],
  35. };
  36. },
  37. created() {},
  38. mounted() {
  39. this.initMap();
  40. },
  41. computed: {},
  42. methods: {
  43. initMap() {
  44. this.map = new Map({
  45. target: "map",
  46. layers: [
  47. new TileLayer({
  48. source: new OSM(),
  49. }),
  50. ],
  51. view: new View({
  52. projection: "EPSG:4326",
  53. center: [104.2974397847, 30.5265062907],
  54. zoom: 18,
  55. }),
  56. });
  57. },
  58. openAnimation() {
  59. this.showAnimation = true;
  60. },
  61. closeAnimation() {
  62. this.$refs.ref_LjOlCarRoute.removeLayer();
  63. this.showAnimation = false;
  64. },
  65. },
  66. };
  67. </script>

组件:LjOlCarRoute/index.vue:

  1. <template>
  2. <div>
  3. <div style="position: fixed; top: 200px; left: 200px">
  4. <el-button @click="startAnimation()">开始</el-button>
  5. <el-button @click="stopAnimation()">暂停</el-button>
  6. </div>
  7. </div>
  8. </template>
  9. <script>
  10. import "ol/ol.css";
  11. import { Map, View, Feature, Overlay } from "ol";
  12. import { Vector as VectorLayer } from "ol/layer";
  13. import { XYZ, OSM, Vector as VectorSource } from "ol/source";
  14. import { Point, LineString } from "ol/geom.js";
  15. import { Icon, Fill, Stroke, Style, Circle } from "ol/style";
  16. import TileLayer from "ol/layer/Tile";
  17. import { getVectorContext } from "ol/render";
  18. export default {
  19. name: "LjOlCarRoute",
  20. props: {
  21. map: {
  22. type: Object,
  23. default: () => {},
  24. required: true,
  25. },
  26. coordinates: {
  27. type: Array,
  28. default: () => [
  29. [115.62, 14.82],
  30. [112.79, 14.82],
  31. [114.6636, 18.2977],
  32. [111.687, 18.897],
  33. [110.3014, 15.063],
  34. ],
  35. },
  36. startPointImage: {
  37. type: String,
  38. default:
  39. "https://smart-garden-manage.oss-cn-chengdu.aliyuncs.com/position.png",
  40. },
  41. endPointImage: {
  42. type: String,
  43. default:
  44. "https://smart-garden-manage.oss-cn-chengdu.aliyuncs.com/position.png",
  45. },
  46. movePointImage: {
  47. type: String,
  48. default:
  49. "https://smart-garden-manage.oss-cn-chengdu.aliyuncs.com/xiaoche.png",
  50. },
  51. speed: {
  52. type: Number,
  53. default: 0.1,
  54. },
  55. },
  56. data() {
  57. return {
  58. route: new LineString(this.coordinates),
  59. geometryMove: {},
  60. featureMove: {},
  61. styles: {
  62. route: new Style({
  63. stroke: new Stroke({
  64. width: 6,
  65. color: [237, 212, 0, 0.8],
  66. }),
  67. }),
  68. startIcon: new Style({
  69. image: new Icon({
  70. anchor: [0.5, 1],
  71. src: this.startPointImage,
  72. scale: 1, //设置大小
  73. }),
  74. }),
  75. endIcon: new Style({
  76. image: new Icon({
  77. anchor: [0.5, 1],
  78. src: this.endPointImage,
  79. scale: 1, //设置大小
  80. }),
  81. }),
  82. featureMove: new Style({
  83. image: new Icon({
  84. // anchor: [0.5, 1],
  85. src: this.movePointImage,
  86. scale: 0.2, //设置大小
  87. }),
  88. }),
  89. },
  90. vectorLayer: {},
  91. distance: 0,
  92. lastTime: 0,
  93. };
  94. },
  95. created() {},
  96. mounted() {
  97. this.initMap();
  98. },
  99. methods: {
  100. initMap() {
  101. this.geometryMove = new Point(this.route.getFirstCoordinate());
  102. this.featureMove = new Feature({
  103. type: "featureMove",
  104. geometry: this.geometryMove,
  105. });
  106. this.vectorLayer = new VectorLayer({
  107. source: new VectorSource({
  108. features: [
  109. new Feature({
  110. type: "route",
  111. geometry: this.route,
  112. }),
  113. this.featureMove,
  114. new Feature({
  115. type: "startIcon",
  116. geometry: new Point(this.route.getFirstCoordinate()),
  117. }),
  118. new Feature({
  119. type: "endIcon",
  120. geometry: new Point(this.route.getLastCoordinate()),
  121. }),
  122. ],
  123. }),
  124. style: (feature) => {
  125. return this.styles[feature.get("type")];
  126. },
  127. });
  128. this.map.addLayer(this.vectorLayer);
  129. },
  130. moveFeature(e) {
  131. let time = e.frameState.time;
  132. this.distance =
  133. (this.distance + (this.speed * (time - this.lastTime)) / 1000) % 1; //%2表示:起止止起;%1表示:起止起止
  134. this.lastTime = time;
  135. const currentCoordinate = this.route.getCoordinateAt(
  136. this.distance > 1 ? 2 - this.distance : this.distance
  137. );
  138. this.geometryMove.setCoordinates(currentCoordinate);
  139. const vectorContext = getVectorContext(e);
  140. vectorContext.setStyle(this.styles.featureMove);
  141. vectorContext.drawGeometry(this.geometryMove);
  142. this.map.render();
  143. },
  144. startAnimation() {
  145. this.lastTime = Date.now();
  146. this.vectorLayer.on("postrender", this.moveFeature);
  147. this.featureMove.setGeometry(null); //必须用null,不能用{}
  148. },
  149. stopAnimation() {
  150. this.featureMove.setGeometry(this.geometryMove);
  151. this.vectorLayer.un("postrender", this.moveFeature);
  152. },
  153. removeLayer() {
  154. this.map.removeLayer(this.vectorLayer);
  155. this.vectorLayer.getSource().clear();
  156. this.featureMove = {};
  157. this.geometryMove = {};
  158. },
  159. },
  160. };
  161. </script>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/607331
推荐阅读
相关标签
  

闽ICP备14008679号