currentLocation.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <template>
  2. <div class="container">
  3. <div id="amap-container"></div>
  4. <div class="controller">
  5. <div class="in_transit_information">
  6. <span class="item_details">车牌号:</span>
  7. <el-input style="width: 120px;" class="inputStyle" v-model.trim="carNumber"> </el-input>
  8. <el-button type="primary" class="searchstyle" @click="initData();initTimer()">查询</el-button>
  9. </div>
  10. </div>
  11. </div>
  12. </template>
  13. <script>
  14. import { shallowRef } from "@vue/reactivity";
  15. import { lazyAMapApiLoaderInstance } from "vue-amap";
  16. import { sjTime, stringToDate } from "@/utils/sharedJsFile";
  17. import Slider from "./slider.vue";
  18. Vue.use(Slider);
  19. import Vue from "vue";
  20. export default {
  21. name: "PathView",
  22. setup() {
  23. const map = shallowRef(null);
  24. return {
  25. map
  26. };
  27. },
  28. watch: {
  29. speedVal: {
  30. deep: true,
  31. handler(val, oldVal) {
  32. let that = this;
  33. if (that.pathNavigator != null) {
  34. that.pathNavigator.setSpeed(100 * val);
  35. }
  36. }
  37. }
  38. },
  39. data() {
  40. return {
  41. //查询车牌号
  42. carNumber: "",
  43. //地图组件
  44. map:null,
  45. path:[],
  46. pointmarker:null,
  47. //定时器
  48. timer: null,
  49. };
  50. },
  51. created() {
  52. let that =this;
  53. that.initMap(105.602725,37.076636)
  54. },
  55. mounted() {
  56. },
  57. methods: {
  58. //初始化数据
  59. initData() {
  60. let that = this;
  61. let regExp = /(^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$)/;
  62. if (that.carNumber == "") {
  63. this.$message.error("车牌号不能为空!");
  64. return;
  65. }else if(!regExp.test(that.carNumber)){
  66. this.$message.error("请输入正确的车牌号!");
  67. return;
  68. }
  69. that.axios
  70. .get("/api/v1/otms/getCurrentLocation?capcityNumber="+that.carNumber)
  71. .then(res => {
  72. console.log(res.data.data)
  73. if(res.data.data=='-1'){
  74. this.$message.error('运输订单未关闭,自提车辆无权查看!');
  75. }else if(res.data.data.status!='1006'){
  76. that.map.setCenter([res.data.data.result.lon,res.data.data.result.lat]);
  77. //画车
  78. that.initMarkes( 45,60, require("@/assets/img/car1.png"),res.data.data.result.lon,res.data.data.result.lat,"现在所在位置");
  79. //给路径加点
  80. let point=new AMap.LngLat(res.data.data.result.lon, res.data.data.result.lat);
  81. if(that.path.length<=0 || !that.path[that.path.length-1].equals(point)){
  82. that.path.push(point);
  83. }
  84. console.log(that.path);
  85. //初始化轨迹
  86. that.initPolyline();
  87. //显示窗体
  88. that.initCustomMarkes(that.carNumber,res.data.data.result.adr,res.data.data.result.lon,res.data.data.result.lat);
  89. }else{
  90. this.$message.error("车辆暂时无GPS");
  91. }
  92. });
  93. },
  94. //初始化地图
  95. initMap(lon,lat) {
  96. lazyAMapApiLoaderInstance.load().then(() => {
  97. let that = this;
  98. that.map = new AMap.Map("amap-container", {
  99. //设置地图容器id
  100. viewMode: "2D", //是否为2D地图模式
  101. zoom: 18, //初始化地图级别
  102. center: [lon,lat] //初始化地图中心点位置105.602725,37.076636
  103. });
  104. });
  105. },
  106. //创建简单的标记
  107. initMarkes(weight, height, image, lon, lat, title) {
  108. let that = this;
  109. //图标标记点
  110. let pointicon = new AMap.Icon({
  111. size: new AMap.Size(weight, height), // 图标尺寸
  112. image: image, // Icon的图像
  113. imageOffset: new AMap.Pixel(2, 10), // 图像相对展示区域的偏移量,适于雪碧图等
  114. imageSize: new AMap.Size(weight, height) // 根据所设置的大小拉伸或压缩图片
  115. });
  116. // 创建一个 Marker 实例:
  117. var pointmarker = new AMap.Marker({
  118. position: new AMap.LngLat(lon, lat), // 经纬度对象,也可以是经纬度构成的一维数组[116.39, 39.9]
  119. icon: pointicon,
  120. title: title
  121. });
  122. // 将创建的点标记添加到已有的地图实例:
  123. that.pointmarker=pointmarker;
  124. that.map.add(pointmarker);
  125. },
  126. //初始化窗体
  127. initCustomMarkes(title, details, lon, lat) {
  128. let that = this;
  129. //自定义窗体内容
  130. var content = [
  131. "<div style='top:1px;width: 180px; background-color:rgba(22, 160, 133, 1);' ><font color='white'>" +
  132. title +
  133. "</font>",
  134. "<div style='background-color:rgba(22, 160, 133, 1);'><font color='white'>" +
  135. details +
  136. "</font></div></div>"
  137. ];
  138. // 创建 infoWindow 实例
  139. var infoWindow = new AMap.InfoWindow({
  140. content: content.join("<br>"),//传入 dom 对象,或者 html 字符串
  141. offset:new AMap.Pixel(0,-20)//修改信息差窗体偏移
  142. });
  143. // 打开信息窗体
  144. infoWindow.open(that.map, [lon, lat]);
  145. },
  146. //初始化轨迹
  147. initPolyline() {
  148. let that = this;
  149. // 创建一个 Polyline 实例:
  150. var polyline = new AMap.Polyline({
  151. path: that.path,
  152. borderWeight: 2, // 线条宽度,默认为 1
  153. strokeColor: '#18BFA6', // 线条颜色
  154. lineJoin: 'round' ,// 折线拐点连接处样式
  155. arrowIconPath: require("@/assets/img/traffic_texture_darkred-pass.png")//箭头图片,似乎不支持
  156. });
  157. // 将创建的线路添加到已有的线路中去:
  158. that.map.add(polyline);
  159. },
  160. //定时取得当前位置并加入轨迹中去,点击查询时触发,同时清除path和旧的定时器
  161. initTimer(){
  162. let that=this;
  163. //清除旧的定时器
  164. clearTimeout(this.timer);
  165. //清除path
  166. that.path=[];
  167. //清除所有覆盖物
  168. that.map.clearMap();
  169. //设置定时器
  170. that.timer=setInterval(()=>{
  171. //清除之前车的图标
  172. that.map.remove(that.pointmarker);
  173. that.initData();
  174. },4000);
  175. }
  176. }
  177. };
  178. </script>
  179. <style>
  180. .inputStyle {
  181. position: absolute;
  182. width: 200px;
  183. left: 50px;
  184. }
  185. .controller {
  186. width: 100%;
  187. height: 70px;
  188. background: white;
  189. position: absolute;
  190. z-index: 99;
  191. top: 0;
  192. left: 0;
  193. }
  194. .in_transit_information {
  195. width: 320px;
  196. float: left;
  197. height: 40px;
  198. }
  199. .container {
  200. width: 100%;
  201. height: 100%;
  202. }
  203. span.item_details2 {
  204. position: relative;
  205. top: 20px;
  206. }
  207. .date_picker_style {
  208. position: relative;
  209. left: 50px;
  210. }
  211. .item_details {
  212. position: relative;
  213. top: 5px;
  214. height: 0px;
  215. left: 0px;
  216. }
  217. #amap-container {
  218. position: relative;
  219. width: 100%;
  220. height: 100%;
  221. overflow: hidden;
  222. margin: 0;
  223. font-family: "微软雅黑";
  224. }
  225. /* 进度条 */
  226. .mySlider {
  227. width: 150px;
  228. height: 20px;
  229. display: inline-block;
  230. position: relative;
  231. left: 32px;
  232. }
  233. /* 进度条 */
  234. .mySlider2 {
  235. width: 400px;
  236. height: 20px;
  237. display: inline-block;
  238. position: relative;
  239. left: 32px;
  240. }
  241. .play {
  242. position: relative;
  243. left: 28px;
  244. }
  245. .quickly {
  246. float: right;
  247. position: relative;
  248. left: -20px;
  249. top: 10px;
  250. }
  251. .pause {
  252. position: relative;
  253. left: 28px;
  254. }
  255. .passed-time {
  256. position: relative;
  257. left: 5px;
  258. }
  259. .end-time {
  260. position: relative;
  261. left: 5px;
  262. }
  263. .map-times {
  264. position: relative;
  265. width: 40px;
  266. }
  267. .searchstyle {
  268. position: relative;
  269. left: 45px;
  270. }
  271. .map-control {
  272. float: left;
  273. width: 270px;
  274. }
  275. .driving_information {
  276. height: 40px;
  277. width: 240px;
  278. float: left;
  279. }
  280. </style>