monitor.vue 9.53 KB
<template>
  <div class="info-content">
    <mapContent @refreshCb="refreshCb" @getData="getData" ref="mapContentRefs">
      <template #mapBox>
        <div id="monitorContainer" class="map-container"></div>
      </template>
    </mapContent>
    <div
      class="toggle-bottom-monitor"
      :class="isShowAllList && locationList.length > 2 ? 'show' : 'hidden'"
    >
      <div class="toggle-btn" @click="showHideList">
        <span
          v-if="!isShowAllList || locationList.length <= 2"
          class="ident-icon"
        ></span>
        <a v-else class="icon icon-put-away"></a>
      </div>
      <div class="list-content-monitor">
        <div
          class="list-item"
          v-for="(item, index) in locationList"
          :key="index"
        >
          <div class="index-text">
            <span>{{ index + 1 }}</span>
          </div>
          <div class="right-text">
            <p class="title">{{ item.transferAddress }}</p>
            <p class="time">{{ item.time }}</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { onMounted, ref, nextTick, watch } from "vue";
import mapContent from "@/components/mapContent.vue";
// import AMapLoader from "@amap/amap-jsapi-loader";
import { CONSTANT } from "../constant";
import { GET_CAR_LOCATION_INFO } from "@/api/index";
import { useRoute } from "vue-router";
import MAPGEO from "@/utils/mapgeo";
import { Dialog } from "vant";
import "vant/es/dialog/style";
export default {
  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 listScroll = ref(0);
    const changey = ref(null);
    // var iii = ref(1)
    const refreshCb = (map) => {
      map.clearMap();
      getData();
      polyLineArr.value = [];
      carMarker = null;
      firstPosLng = "";
      firstPosLat = "";
      beginMarker = null;
      polyLine = null;
      locationList.value = [];
      timeoutId.value && clearTimeout(timeoutId.value);
    };
    onMounted(() => {
      mapContentRefs.value.initMap("monitorContainer");
      // AMapLoader.load({
      //   key: CONSTANT.MAP_KEY,
      //   version: "1.4.8",
      //   plugins: CONSTANT.MAP_PLUGINS,
      // }).then((AMap) => {
      //   window.AMap = AMap;
      // });
    });
    watch(changey, (newVal) => {
      let listNode = document.querySelector(".list-content-monitor");
      let flag = false;
      if (listScroll.value === 0) {
        flag = true;
      }
      if (flag && newVal >= 50) {
        isShowAllList.value = false;
      }
      if (
        newVal <= -50 &&
        listNode.scrollTop === 0 &&
        locationList.value.length > 2
      ) {
        // 上滑
        isShowAllList.value = true;
      }
    });
    let starty, endy;
    nextTick(() => {
      let touchNode = document.querySelector(".toggle-bottom-monitor");
      let listNode = document.querySelector(".list-content-monitor");
      const scroll = () => {
        let scroolTop = listNode.scrollTop;
        listScroll.value = scroolTop;
      };
      touchNode.addEventListener("touchstart", function (e) {
        listNode.addEventListener("scroll", scroll(e), false);
        listNode.removeEventListener("scroll", scroll(e), false);
        starty = e.touches[0].pageY;
      });
      //结束点击时获得触点
      touchNode.addEventListener("touchend", function (e) {
        endy = e.changedTouches[0].pageY;
        //判断触点差值
        changey.value = endy - starty;
      });
    });
    // 获取数据
    const getData = (map) => {
      currentMap = map ? map : currentMap;
      GET_CAR_LOCATION_INFO({ sns: routeQuery.sn }).then((res) => {
        if (res.code === 200) {
          if (res.data.length > 0) {
            init(currentMap, res.data);
          } else {
            Dialog({ message: "暂无数据" });
          }
        }
      });
    };
    // marker渲染
    const init = (currentMap, data) => {
      data.forEach((val) => {
        (val.blat =
          val[routeQuery.locationType] && val[routeQuery.locationType].blat),
          (val.blng =
            val[routeQuery.locationType] && val[routeQuery.locationType].blng);
        val.time =
          val[routeQuery.locationType] && val[routeQuery.locationType].dateTime;
      });
      MAPGEO.transferAddressByGroup(
        {
          data: data,
          timeoutIds: [],
        },
        () => {
          data.forEach((val) => {
            // val.blng = +val.blng + Math.random(0,9) * 0.0001 * iii.value
            // val.blat = +val.blat + Math.random(0,9) * 0.0001 * iii.value
            // iii.value = iii.value + 1
            // var lnglat = new window.AMap.LngLat(val.blng, val.blat);
            var lnglat = mapContentRefs.value.getLngLat(val.blng, val.blat);
            polyLineArr.value.push(lnglat);
            locationList.value.push(val);
            if (!carMarker) {
              firstPosLng = val.blng;
              firstPosLat = val.blat;
              carMarker = mapContentRefs.value.mapMarker({
                point: lnglat,
                icon: require("@/assets/images/car.png"),
                sizeW: 17,
                sizeH: 26,
                imgW: 17,
                imgH: 26,
              });
              beginMarker = mapContentRefs.value.mapMarker({
                point: lnglat,
                isAdd: false,
                icon: require("@/assets/icon/begin.png"),
                sizeW: 26,
                sizeH: 36,
                imgW: 26,
                imgH: 36,
              });
              // carMarker = new window.AMap.Marker({
              //   position: lnglat,
              //   content:
              //     '<img src=" ' +
              //     require("@/assets/images/car.png") +
              //     '" style="transform: scale(0.5);" />',
              //   map: currentMap,
              //   offset: new window.AMap.Pixel(-17, -26),
              // });
              // beginMarker = new window.AMap.Marker({
              //   position: lnglat,
              //   content: '<span class="icon icon-begin" ></span>',
              //   map: currentMap,
              //   offset: new window.AMap.Pixel(-17, -26),
              //   visible: false,
              // });
            } else {
              // carMarker.setPosition(lnglat);
              window.mapapi.invoke("markerSetPosition", carMarker, lnglat);
              if (firstPosLng !== val.blng || firstPosLat !== val.blat) {
                // beginMarker.show();
                window.mapapi.invoke("addOverlay", beginMarker);
              }
              if (!polyLine) {
                polyLine = mapapi.invoke("Polyline", polyLineArr, {
                  strokeWeight: 2,
                  strokeColor: "#009944",
                });
                window.mapapi.invoke("addOverlay", polyLine);
                // polyLine = new window.AMap.Polyline({
                //   map: currentMap,
                //   strokeWeight: 2,
                //   strokeColor: "#0b94db",
                //   strokeStyle: "solid",
                //   strokeDasharray: [10, 10],
                // });
              }
              // polyLine.setPath(polyLineArr);
              window.mapapi.invoke("polylineSetPath", polyLineArr.value);
            }
          });
          // currentMap.setFitView();
          window.mapapi.invoke("setViewport", polyLineArr.value, {
            // zoomFactor: -3
          });
          timeoutId.value = setTimeout(function () {
            getData();
          }, 10 * 1000);
        }
      );
    };
    // 显示隐藏实时地址列表数据
    const showHideList = () => {
      isShowAllList.value = !isShowAllList.value;
    };
    return {
      mapContentRefs,
      getData,
      refreshCb,
      init,
      showHideList,
      isShowAllList,
      locationList,
      listScroll,
      changey,
      timeoutId,
    };
  },
};
</script>

<style lang="scss" scoped>
.toggle-bottom-monitor {
  position: fixed;
  width: 100%;
  left: 0px;
  bottom: 0px;
  z-index: 300;
  display: flex;
  border-radius: 20px 20px 0 0;
  align-items: center;
  flex-flow: column;
  background-color: #fff;
  .toggle-btn {
    position: absolute;
    top: -20px;
    .ident-icon {
      display: inline-block;
      width: 56px;
      height: 4px;
      border-radius: 2px;
      background: #a2a2a4;
    }
  }
  .list-content-monitor {
    box-sizing: border-box;
    width: 100%;
    padding: 5px 15px 0 15px;
    font-size: 14px;
    .list-item {
      padding: 14px 0 0 0;
      display: flex;
      .index-text {
        margin-top: 3px;
        min-width: 24px;
      }
      .right-text {
        display: flex;
        flex-flow: column;
        overflow: hidden;
        padding-bottom: 14px;
        width: 100%;
        border-bottom: 1px solid #f0f2f5;
      }
      .title {
        color: #000;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }
      .time {
        color: #666;
        font-size: 12px;
        margin-top: 7px;
      }
    }
    :last-child {
      border: none;
    }
  }
}
.toggle-bottom-monitor.hidden {
  max-height: 135px;
  transition: max-height 0.3s;
}
.toggle-bottom-monitor.show {
  max-height: 60%;
  transition: max-height 0.3s;
  .list-content-monitor {
    overflow-y: scroll;
  }
}
</style>