作者 小范

新增多车定位界面

@@ -23,3 +23,5 @@ export const DoneTask = params => { return http.post(`${baseServiceURL}/map/loca @@ -23,3 +23,5 @@ export const DoneTask = params => { return http.post(`${baseServiceURL}/map/loca
23 //根据用户查询所属出勤车辆 23 //根据用户查询所属出勤车辆
24 export const selectNameList = params => { return axios.get(`${baseServiceURL}/map/location/selectNameList`, { params: params }); }; 24 export const selectNameList = params => { return axios.get(`${baseServiceURL}/map/location/selectNameList`, { params: params }); };
25 25
  26 +//多车定位
  27 +export const queryMulVel = params => { return axios.get(`${baseServiceURL}/map/location/queryMulVel`, { params: params }); };
  1 +<template>
  2 + <div id="mapContainer"></div>
  3 +</template>
  4 +
  5 +<script>
  6 + import AMapLoader from '@amap/amap-jsapi-loader';
  7 + import {queryMulVel} from '../../api/consigner/vehicle';
  8 +
  9 + export default {
  10 + data() {
  11 + return {
  12 + mapInstance: null,
  13 + markers:[]
  14 + };
  15 + },
  16 + methods: {
  17 + fetchDataAndUpdateMap() {
  18 + // 请求后端数据
  19 + queryMulVel().then((response) => {
  20 + const res = response.data;
  21 + const firstVcl = res.data.data.firstVcl; // 获取首个车辆信息
  22 + const others = res.data.data.others; // 获取其他车辆信息
  23 +
  24 + // 清除之前的标记
  25 + this.markers.forEach(marker => {
  26 + marker.setMap(null);
  27 + });
  28 + this.markers = [];
  29 +
  30 + // 添加首个车辆标记
  31 + if (firstVcl && firstVcl.lon && firstVcl.lat) {
  32 + const marker = new AMap.Marker({
  33 + position: [firstVcl.lon / 600000.0, firstVcl.lat / 600000.0],
  34 + map: this.mapInstance,
  35 + title: firstVcl.vno,
  36 + label: {
  37 + content: firstVcl.vno,
  38 + offset: new AMap.Pixel(0, -20) // 偏移量,使标题显示在标记的上方
  39 + }
  40 + });
  41 + this.markers.push(marker);
  42 + }
  43 + if (others) {
  44 + others.forEach(vehicle => {
  45 + if (vehicle.lon && vehicle.lat) {
  46 + const marker = new AMap.Marker({
  47 + position: [vehicle.lon / 600000.0, vehicle.lat / 600000.0],
  48 + map: this.mapInstance,
  49 + title: vehicle.vno,
  50 + label: {
  51 + content: vehicle.vno,
  52 + offset: new AMap.Pixel(0, -20) // 偏移量,使标题显示在标记的上方
  53 + }
  54 + });
  55 + this.markers.push(marker);
  56 + }
  57 + });
  58 + }
  59 + // 自动调整地图视野,确保所有标记都可见
  60 + /*if (this.markers.length > 0) {
  61 + const bounds = new AMap.Bounds();
  62 + this.markers.forEach(marker => {
  63 + bounds.extend(marker.getPosition());
  64 + });
  65 + this.mapInstance.setBounds(bounds);
  66 + }*/
  67 + if(firstVcl && firstVcl.lon && firstVcl.lat){
  68 + this.mapInstance.setCenter([firstVcl.lon / 600000.0,firstVcl.lat / 600000.0])
  69 + }
  70 + }).catch(error => {
  71 + console.error('Failed to fetch data:', error);
  72 + });
  73 + }
  74 + },
  75 + mounted() {
  76 + AMapLoader.load({
  77 + key: 'fdb27c13681d084e85ff8457b5cbe540',
  78 + version: '2.0',
  79 + plugins: [],
  80 + }).then((AMap) => {
  81 + this.mapInstance = new AMap.Map('mapContainer', {
  82 + zoom: 5,
  83 + center: [116.397428, 39.90923], // 默认中心点位置
  84 + //viewMode: '3D', // 使用3D视图
  85 + //showIndoorMap: false,
  86 + //showLabel: false,
  87 + // mapStyle: 'amap://styles/dark',
  88 + //mapStyle: 'amap://styles/fresh',
  89 + });
  90 +
  91 + // 首次加载数据并更新地图
  92 + this.fetchDataAndUpdateMap();
  93 +
  94 + // 启动定时器,每10分钟刷新一次数据并更新地图
  95 + setInterval(() => {
  96 + this.fetchDataAndUpdateMap();
  97 + }, 10 * 60 * 1000);
  98 + }).catch((err) => {
  99 + console.error('Failed to load AMap:', err);
  100 + });
  101 + },
  102 + };
  103 +</script>
  104 +<style>
  105 + #mapContainer {
  106 + width: 100%;
  107 + height: 100vh;
  108 + }
  109 +</style>