monitor.vue 6.24 KB
<template>
  <div>
    <mapContent @refreshCb="refreshCb" @getData="getData" ref="mapContentRefs">
      <template #mapBox>
        <div id="monitorContainer" class="map-container"></div>
      </template>
    </mapContent>
    <transition name="van-fade"> </transition>
    <div class="toggle-bottom" :class="isShowAllList ? '' : 'hidden'">
      <div class="toggle-btn" @click="showHideList">
        <van-icon
          :name="
            !isShowAllList && locationList.length > 2
              ? 'arrow-up'
              : 'arrow-down'
          "
        />
      </div>
      <div class="list-content">
        <div
          class="list-item"
          v-for="(item, index) in locationList"
          :key="index"
        >
          <p class="title">{{ item.transferAddress }}</p>
          <p class="time">{{ item.time }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { defineComponent, onMounted, ref } from "vue";
import mapContent from "@/components/mapContent.vue";
import AMapLoader from "@amap/amap-jsapi-loader";
import { CONSTANT } from "../constant";
import { GET_RESIDENT_DATA } from "@/api/index";
import { useRoute } from "vue-router";
import MAPGEO from "@/utils/mapgeo";
import { Dialog } from "vant";
import "vant/es/dialog/style";
export default defineComponent({
  name: "MonitorPage",
  components: {
    mapContent,
  },
  setup() {
    const route = useRoute();
    const mapContentRefs = ref(null);
    const isShowAllList = ref(false);
    const locationList = ref([]);
    let currentMap = null;
    let routeQuery = route.query;
    let polyLineArr = ref([]);
    let carMarker = null;
    let firstPosLng = "";
    let firstPosLat = "";
    let beginMarker = null;
    let polyLine = null;
    const timeoutId = ref(null);
    const refreshCb = (map) => {
      map.clearMap();
      getData();
      polyLineArr.value = [];
      carMarker = null;
      firstPosLng = "";
      firstPosLat = "";
      beginMarker = null;
      polyLine = null;
      locationList.value = [];
      timeoutId.value && clearTimeout(timeoutId.value);
    };
    onMounted(() => {
      AMapLoader.load({
        key: CONSTANT.MAP_KEY,
        version: "1.4.8",
        plugins: CONSTANT.MAP_PLUGINS,
      }).then((AMap) => {
        window.AMap = AMap;
        mapContentRefs.value.initMap("monitorContainer");
      });
    });
    // 获取数据
    const getData = (map) => {
      currentMap = map ? map : currentMap;
      init(currentMap);
      GET_RESIDENT_DATA({ sns: routeQuery.sn }).then((res) => {
        if (res.code === 200) {
          if (res.data.length > 0) {
            init(currentMap);
          } else {
            Dialog({ message: "暂无数据" });
          }
        }
      });
    };
    // marker渲染
    const init = (currentMap) => {
      let data = [
        {
          totalDuration: 2032460,
          totalTimes: 1,
          time: "2022-07-01 15:30:26",
          longitude: "114.0736",
          latitude: "30.606778",
          blat: "30.610309854203035",
          blng: "114.08589711310412",
          elat: "30.604489843507423",
          elng: "114.07934400356585",
          name: "鄂ADB1383",
          standNo: "LA9XL44B0HBLG6389",
          sn: "1500628541",
          address: "湖北省武汉市东西湖区慈惠路毛家台(公交站)",
        },
      ];
      MAPGEO.transferAddressByGroup(
        {
          data: data,
          timeoutIds: [],
        },
        () => {
          data.forEach((val) => {
            var lnglat = new window.AMap.LngLat(val.elng, val.elat);
            polyLineArr.value.push(lnglat);
            locationList.value.push(val);
            if (!carMarker) {
              firstPosLng = val.elng;
              firstPosLat = val.elat;
              carMarker = new window.AMap.Marker({
                position: lnglat,
                content: '<span class="icon icon-car"></span>',
                map: currentMap,
                offset: new window.AMap.Pixel(-8.5, -13),
              });
              beginMarker = new window.AMap.Marker({
                position: lnglat,
                content: '<span class="icon icon-begin"></span>',
                map: currentMap,
                offset: new window.AMap.Pixel(-8.5, -13),
                visible: false,
              });
            } else {
              carMarker.setPosition(lnglat);
              if (firstPosLng !== val.elng || firstPosLat !== val.elat) {
                beginMarker.show();
              }
              if (!polyLine) {
                polyLine = new window.AMap.Polyline({
                  map: currentMap,
                  strokeWeight: 2,
                  strokeColor: "#0b94db",
                  strokeStyle: "solid",
                  strokeDasharray: [10, 10],
                });
              }
              polyLine.setPath(polyLineArr);
            }
          });
          currentMap.setFitView();
          timeoutId.value = setTimeout(function () {
            init(currentMap);
          }, 10 * 1000);
          if (locationList.value.length > 6) locationList.value.shift();
        }
      );
    };
    // 显示隐藏实时地址列表数据
    const showHideList = () => {
      isShowAllList.value = !isShowAllList.value;
    };
    return {
      mapContentRefs,
      getData,
      refreshCb,
      init,
      showHideList,
      isShowAllList,
      locationList,
    };
  },
});
</script>

<style lang="scss" scoped>
.toggle-bottom {
  position: fixed;
  width: 100%;
  left: 0px;
  bottom: 0px;
  z-index: 300;
  display: flex;
  border-radius: 20px;
  max-height: 455px;
  align-items: center;
  flex-flow: column;
  background-color: #fff;
  padding-top: 10px;
  .list-content {
    padding-left: 10px;
    overflow-y: scroll;
    .list-item {
      padding: 5px 10px 5px 0;
      border-bottom: 1px solid #ececec;
      .title {
        color: #000;
        font-size: 16px;
      }
      .time {
        color: #666;
        .navigate {
          padding-left: 16px;
          background-size: 12px 12px;
          float: right;
          margin-left: 10px;
        }
        .copy-adr {
          padding-left: 16px;
          background-size: 12px 12px;
          float: right;
        }
      }
    }
    :last-child {
      border: none;
    }
  }
}
.toggle-bottom.hidden {
  max-height: 160px;
}
</style>