mapContent.vue 3.77 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";
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) => {
      let check = () => {
        checkLoadedTimeoutId && window.clearTimeout(checkLoadedTimeoutId);
        let AMap = window.AMap;
        if (AMap && typeof AMap.Map == "function") {
          window.AMap = AMap;
          init(id);
        } else {
          checkLoadedTimeoutId = check();
        }
      };
      checkLoadedTimeoutId = check();
    };
    const init = (id) => {
      map = new window.AMap.Map(id);
      isUploaded.value = true;
      context.emit("isUploaded", true);
      context.emit("getData", map);
      return map;
    };
    let currMarker = null;
    // 获取定位
    const getLocation = () => {
      isGetLocation.value = true;
      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 refresh = () => {
      context.emit("refreshCb", map);
    };
    return {
      initMap,
      init,
      checkLoadedTimeoutId,
      getLocation,
      satelActive,
      roadActive,
      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>