index.vue 3.67 KB
<template>
  <div id="mapContainer" class="map-container"></div>
</template>
<script>
import { CONSTANT } from "@/amap/map";
import mapService from "@/amap/mapService";
import AMapLoader from "@amap/amap-jsapi-loader";
import "@/assets/spritesmith-generated/sprite.css";
import "@/assets/styles/monitor.scss";
/**
 * 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: "",
    };
  },
  mounted() {
    console.log(mapService);
    this.initMap();
  },
  methods: {
    // 初始化地图
    initMap() {
      let _this = this;
      AMapLoader.load({
        key: CONSTANT.MAP_KEY, // 申请好的Web端开发者Key,首次调用 load 时必填
        version: "1.4.2", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
        plugins: CONSTANT.MAP_PLUGINS, // 需要使用的的插件列表,如比例尺'AMap.Scale'等
      }).then((AMap) => {
        let _amap = new AMap.Map("mapContainer", {
          //设置地图容器id
          viewMode: "3D", //是否为3D地图模式
        });
        _amap.on("complete", function () {
          console.log(_this.info);
          if (_this.info && (_this.info.bsLng || _this.info.gpsLng)) {
            _this.drawMaker();
          }
        });
        _this.mapObj = _amap;
      });
    },

    // 激活位置
    drawMaker() {
      let data = this.info;
      const MAP = this.mapObj;
      const marker = mapService.createCarMarker(
        [
          parseFloat(data.lng || data.bsLng),
          parseFloat(data.lat || data.bsLat),
        ],
        MAP
      );
      const infoWindow = mapService.bindActiveLocationInfoWindow(
        marker,
        MAP,
        data,
        true
      );

      // 设置图标
      mapService.setDefaultIcon(marker, data.vehicleIcon);

      //if (autoOpenLastInfoWindow) {
      // /EventBus.$emit("uiMap.monitor.openInfoWindow");
      mapService.openActiveInfoWindow(MAP, marker, infoWindow);
      //}

      //EventBus.$emit("uiMap.monitor.drawDone");
    },
  },
};
</script>
<style lang="scss" scoped>
p {
  margin: 0;
  padding: 0;
}
.map-container {
  width: 100%;
  min-width: 600px;
  height: 400px;
  font-size: 13px;
}
</style>