index.vue 7.61 KB
<template>
  <div>
    <div id="mapContainer" class="map-container"></div>
  </div>
</template>
<script>
// import mapService from "@/amap/mapService";
import "@/assets/spritesmith-generated/sprite.css";
import "@/assets/styles/monitor.scss";
import GLMapSdk from "@/utils/glsk-mapapi-sdk";
/**
 * address: gps地址
 * bsAddress:基站地址
 * bsLat,bsLng:基站经纬度
 * gpsLat,gpsLng:gps经纬度
 * name:车主姓名
 * standNo:车架号
 * time:定位时间
 */
export default {
  name: "MapLocation",
  props: {
    info: {
      type: Object,
      default: function () {
        return {
          // activeDate: "2022-02-22 17:57:23",
          // address: "广东省深圳市南山区科技园",
          // bsAddress: "广东省深圳市南山区科技园",
          // bsLat: "22.551794",
          // bsLng: "113.941085",
          // bsTime: null,
          // deviceType: null,
          // deviceTypeStr: null,
          // elat: "22.550615",
          // elng: "113.938668",
          // finalBsTime: null,
          // finalGpsTime: null,
          // gpsLat: "22.550615",
          // gpsLng: "113.938668",
          // lastBsAddress: "",
          // lastGpsAddress: "广东省深圳市南山区科技园",
          // lastPoiAddress: "广东省深圳市南山区科技园",
          // lastPoiTime: "2018-10-15 16:14:11",
          // lat: "22.550615",
          // lng: "113.938668",
          // name: "仲利测试1",
          // ownerName: "仲利测试1",
          // poiType: "GPS",
          // shopName: "仲利国际租赁有限公司",
          // sn: "7140626339",
          // source: null,
          // sourceStr: null,
          // standNo: "LGBF5AE08FR305952",
          // time: '2022-02-22 17:57:23',
          // type: 2,
          // vin: "LGBF5AE08FR305952",
          // workOrderNo: "481985751282679808",
        };
      },
    },
  },

  data() {
    return {
      mapObj: "",
      zoom: 10,
      mapapi: "",
    };
  },
  created() {
    this.initMap();
  },
  methods: {
    async initMap() {
      new GLMapSdk(1, async (mapapi) => {
        this.mapapi = mapapi;
        const map = await mapapi.invoke("Map", "mapContainer");
        const { lng, lat } = this.info;
        mapapi.invoke("centerAndZoom", mapapi.invoke("Point", lng, lat), this.zoom);
        //向地图中添加缩放控件
        var ctrlNav = new window.BMap.NavigationControl({
          anchor: BMAP_ANCHOR_TOP_LEFT,
          type: BMAP_NAVIGATION_CONTROL_LARGE,
        });
        //地图缩放控件
        map.addControl(ctrlNav);
        // mapapi.invoke('enableScrollWheelZoom')
        this.drawMaker(mapapi, map);
      });
    },

    // 激活位置
    drawMaker(mapapi, map) {
      let data = this.info;
      const lng = parseFloat(data.lng || data.bsLng);
      const lat = parseFloat(data.lat || data.bsLat);
      var marker = mapapi.invoke("Marker", mapapi.invoke("Point", lng, lat), {
        icon: mapapi.invoke(
          "Icon",
          require("@/assets/images/icon_car.png"),
          mapapi.invoke("Size", 60, 60),
          {
            anchor: mapapi.invoke("Size", 10, 15),
            infoWindowAnchor: mapapi.invoke("Size", 10, 0),
          }
        ),
      });
      this.bindActiveLocationInfoWindow(marker, map, data);

      mapapi.invoke("addOverlay", marker);
    },
    // 创建位置点弹框
    createActiveInfo(info) {
      let msg = "报警";
      // 激活
      if (info.type == 1) {
        msg = "激活";
      }

      // 定位
      if (info.type == 2) {
        msg = "定位";
      }
      let _html = `<h3 class="map-infowindow-title">${info.name}/${info.standNo}</h3>
        <div class="map-infowindow-ct">
            <p><i class="icon icon-info_device"></i> 设备号: <span class="des">${
              info.sn
            } (${info.source == 1 ? "有线" : "无线"})</span></p>`;

      // 是否包含断电、光感报警
      if (info.alarmTypeSet && info.alarmTypeSet.length > 0) {
        let tg = "";
        let cHmtl = "";
        let typeList = {
          1: "断电",
          99: "光感",
        };
        info.alarmTypeSet.forEach((alarmType) => {
          tg += `<a class="btn_danger">${typeList[alarmType]}</a>`;
        });
        cHmtl =
          '<p><i class="icon icon-info_device_alarm"></i><span>报警状态:</span>' +
          tg +
          "</p>";
        _html += cHmtl;
      }

      _html += `<p><i class="icon icon-info_time"></i>${msg}时间: <span class="des"data-time>${
        info.time
      }</span></p>
        <p style="display:${
          info.bsLng ? "flex" : "none"
        }"><i class="icon icon-info_type"></i>定位类型: <a class="locate-btn active" id="locateTypeGps">GPS</a><a class="locate-btn" id="locateTypeBase">基站</a></p>
        <p class="long_p"><i class="icon icon-info_location"></i><span>${msg}地址:</span><span class="des" data-transferAddress>${
        info.address
      }</span></p>
    </div>`;
      return _html;
    },
    // 位置点弹框
    bindActiveLocationInfoWindow(marker, map, info) {
      var self = this;
      var infoDiv = document.createElement("div");
      infoDiv.innerHTML = self.createActiveInfo(info);

      // gps位置信息
      function fillGPS() {
        infoDiv.querySelector("[data-time]").textContent = info.time;
        infoDiv.querySelector("[data-transferAddress]").textContent = info.address;
        infoDiv.querySelector("#locateTypeGps").classList.add("active");
        infoDiv.querySelector("#locateTypeBase").classList.remove("active");
        marker.setPosition(self.mapapi.invoke("Point", info.lng, info.lat));
        map.openInfoWindow(marker.infoWindow, marker.getPosition());
      }

      // 基站位置信息
      function fillBase() {
        infoDiv.querySelector("[data-time]").textContent = info.bsTime;
        infoDiv.querySelector("[data-transferAddress]").textContent = info.bsAddress;
        infoDiv.querySelector("#locateTypeGps").classList.remove("active");
        infoDiv.querySelector("#locateTypeBase").classList.add("active");
        console.log(self.mapapi.invoke("Point", info.bsLng, info.bsLat), "基站点位置", map);
        marker.setPosition(self.mapapi.invoke("Point", info.bsLng, info.bsLat));
        map.openInfoWindow(marker.infoWindow, marker.getPosition());
      }

      infoDiv.querySelector("#locateTypeGps").onclick = function (e) {
        fillGPS();
      };
      infoDiv.querySelector("#locateTypeBase").onclick = function (e) {
        fillBase();
      };

      if (info.lng) {
        fillGPS();
      }
      if (!info.lng && info.bsLng) {
        fillBase();
      }

      return this.createActiveInfoWindow(marker, map, infoDiv);
    },

    // 创建位置点弹框
    createActiveInfoWindow(marker, map, div, isInfoOpen, isInfoAutoMove) {
      let opts = {
        width: 380, // 信息窗口宽度
        height: 170, // 信息窗口高度
      };
      let infoWindow = this.mapapi.invoke("InfoWindow", div, opts);
      marker.infoWindow = infoWindow;
      map.openInfoWindow(infoWindow, marker.getPosition());

      //给Marker绑定单击事件
      marker.addEventListener("click", markerClick);
      function markerClick(e) {
        map.openInfoWindow(infoWindow, marker.getPosition());
      }
      return infoWindow;
    },

    // 打开激活位置点弹框
    openActiveInfoWindow(map, makers, infoWindow) {
      const MAP = map;
      const lnglat = makers.getPosition();
      infoWindow.open(MAP, lnglat);
      MAP.setZoomAndCenter(MAP.getZoom(), lnglat);
    },
  },
};
</script>
<style lang="scss" scoped>
p {
  margin: 0;
  padding: 0;
}
.map-container {
  width: 100%;
  min-width: 600px;
  height: 400px;
  font-size: 13px;
}
</style>