mapContent.vue 6.18 KB
<template>
  <div class="map-box">
    <slot name="mapBox"></slot>
    <div v-show="isUploaded">
      <button
        class="map-cmd-icon location"
        @click="getLocation"
        v-preventReClick
      >
        <span v-if="!isGetLocation" class="icon icon-location"></span>
        <van-loading v-else color="#1989fa" />
      </button>
      <button
        class="map-cmd-icon refresh-car"
        @click="refresh"
        v-preventReClick
      >
        <span class="icon icon-refresh-car"></span>
      </button>
    </div>
  </div>
</template>

<script>
import { onMounted, ref } from "vue";
import { Toast } from "vant";
import "vant/es/toast/style";
import GLMap from "@/utils/glsk-mapapi-sdk.js";
export default {
  name: "mapContent",
  props: {},
  setup(props, context) {
    let map = null;
    const isUploaded = ref(false);
    let checkLoadedTimeoutId = "";
    const satelActive = ref(null);
    const roadActive = ref(null);
    const isGetLocation = ref(false);
    onMounted(() => {});
    const initMap = (id, version = 3) => {
      return new Promise((resolve) => {
        let check = () => {
          checkLoadedTimeoutId && window.clearTimeout(checkLoadedTimeoutId);
          if(GLMap) {
            new GLMap({ version }, mapapi => {
            // new GLMap(mapapi => {
              window.mapapi = mapapi;
              init(id);
              resolve()
            })
          } else {
            checkLoadedTimeoutId = check();
          }
        };
        checkLoadedTimeoutId = check();
      });
    };
    const init = (id) => {
      map = window.mapapi.invoke('Map', id);
      var point = mapapi.invoke('Point', 116.2317, 39.5427)
      window.mapapi.invoke('LocalCity', (data) => {
        if(data) {
          point = data.center
        }
        mapapi.invoke('centerAndZoom', point, 10);
        isUploaded.value = true;
        context.emit("isUploaded", true);
        context.emit("getData", map);
        return map;
      })
    };
    let currMarker = null;
    // 获取定位
    const getLocation = () => {
      isGetLocation.value = true;
      window.mapapi.invoke('Geolocation', (data) => {
        if(data) {
        if (currMarker) {
            // currMarker.setMap(null);
            window.mapapi.invoke('removeOverlay', currMarker)
          }
          isGetLocation.value = false;
          currMarker = window.mapapi.invoke('Marker', data.point, {
            icon: window.mapapi.invoke('Icon', require("@/assets/images/curr-marker.gif"), window.mapapi.invoke('Size', 24, 24)),
            // offset: window.mapapi.invoke('Pixel', -12, -12),
          })
          window.mapapi.invoke('addOverlay', currMarker)
          window.mapapi.invoke('panTo', data.point)
          // currMarker = new window.AMap.Marker({
          //   position: data.position,
          //   content: '<span class="curr-marker"></span>',
          //   map: map,
          //   offset: new window.AMap.Pixel(-12, -12),
          // });
          // map.setFitView();
        } else {
          Toast("获取定位失败");
          isGetLocation.value = false;
        }
      }, {
        enableHighAccuracy: true,
        timeout: 10000,
      })
      // map.plugin("AMap.Geolocation", function () {
      //   var geolocation = new window.AMap.Geolocation({
      //     enableHighAccuracy: true,
      //     timeout: 10000,
      //     buttonOffset: new window.AMap.Pixel(10, 20),
      //     zoomToAccuracy: true,
      //     buttonPosition: "RB",
      //   });

      //   geolocation.getCurrentPosition();
      //   window.AMap.event.addListener(geolocation, "complete", (data) => {
      //     if (currMarker) {
      //       currMarker.setMap(null);
      //     }
      //     isGetLocation.value = false;
      //     currMarker = new window.AMap.Marker({
      //       position: data.position,
      //       content: '<span class="curr-marker"></span>',
      //       map: map,
      //       offset: new window.AMap.Pixel(-12, -12),
      //     });
      //     map.setFitView();
      //   });
      //   window.AMap.event.addListener(geolocation, "error", () => {
      //     Toast("获取定位失败");
      //     isGetLocation.value = false;
      //   });
      // });
    };

    const getLngLat = (lng, lat) => {
      return window.mapapi.invoke('Point', lng, lat)
    };
    const mapMarker = ({point, Animation = null, isAdd = true, icon = require("@/assets/images/curr-marker.gif"), sizeW = 52, sizeH = 52, imgW = 52, imgH = 52 }) => {
      var currMarker = window.mapapi.invoke('Marker', point, {
          icon: window.mapapi.invoke('Icon', icon, window.mapapi.invoke('Size', sizeW, sizeH), {
            imageSize: window.mapapi.invoke('Size', imgW, imgH)
          }),
        })
        // console.log(Animation, 'Animation---')
        // Animation && window.mapapi.invoke('markerSetAnimation', Animation)
        if(isAdd) {
          window.mapapi.invoke('addOverlay', currMarker)
          window.mapapi.invoke('panTo', point)
        }
        return currMarker
    };
    // 刷新
    const refresh = () => {
      context.emit("refreshCb", map);
    };
    return {
      initMap,
      init,
      checkLoadedTimeoutId,
      getLocation,
      satelActive,
      roadActive,
      getLngLat,
      mapMarker,
      refresh,
      isUploaded,
      isGetLocation,
    };
  },
};
</script>

<style lang="scss" scoped>
.map-box {
  height: 100%;
}
.map-cmd-icon {
  width: 40px;
  height: 40px;
  background: #fff;
  border-radius: 50%;
  position: fixed;
  z-index: 101;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0px 5px 12px 0px rgba(0, 0, 0, 0.12);
}
.location {
  bottom: 160px;
  right: 20px;
}
.refresh-car {
  bottom: 220px;
  right: 20px;
}

.map-type-list {
  position: fixed;
  top: 50px;
  right: 10px;
  z-index: 101;
  background-color: #fff;
  border: 1px solid #eee;
  padding: 4px;
  a {
    display: block;
    border-bottom: 1px solid #eee;
  }
  :last-child {
    border-bottom: 0;
  }
}
.map-size-list {
  position: fixed;
  top: 150px;
  right: 10px;
  z-index: 101;
  background-color: #fff;
  border: 1px solid #eee;
  padding: 4px;
  a {
    display: block;
    border-bottom: 1px solid #eee;
  }
  :last-child {
    border-bottom: 0;
  }
}
</style>