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
| import { AMapManager } from "vue-amap"; let amapManager = new AMapManager(); export default { name: "Track", data() { const that = this; return { map: null, amapManager, events: { complete() { that.map = that.amapManager.getMap(); }, click: (e) => { const { lng, lat } = e.lnglat; console.log(`当前点击点的经纬度为:${lng},${lat}`); }, }, polyline: null, passedPolyline: null, marker: null, }; }, methods: { addTrackBtn() { if (this.polyline) { let polylines = [this.polyline, this.passedPolyline]; this.map.remove(polylines); this.map.remove(this.marker); }
let lineArr = [ [116.478935, 39.997761], [116.478939, 39.997825], [116.478912, 39.998549], [116.478912, 39.998549], [116.478998, 39.998555], [116.478998, 39.998555], [116.479282, 39.99856], [116.479658, 39.998528], [116.480151, 39.998453], [116.480784, 39.998302], [116.480784, 39.998302], [116.481149, 39.998184], [116.481573, 39.997997], [116.481863, 39.997846], [116.482072, 39.997718], [116.482362, 39.997718], [116.483633, 39.998935], [116.48367, 39.998968], [116.484648, 39.999861], ]
this.marker = new AMap.Marker({ map: this.map, });
this.polyline = new AMap.Polyline({ map: this.map, path: lineArr, showDir: true, strokeColor: "#28F", strokeWeight: 6, });
this.passedPolyline = new AMap.Polyline({ map: this.map, strokeColor: "#AF5", strokeWeight: 6, });
let _this = this; this.marker.on("moving", function (e) { _this.passedPolyline.setPath(e.passedPath); });
let distance = Math.round( AMap.GeometryUtil.distanceOfLine(lineArr) );
this.map.setFitView();
this.marker.moveAlong( lineArr, (distance / 200) * 90, function (k) { return k; }, true ); } } };
|