mapContent.vue 3.26 KB
<template>
  <div class="map-box">
    <slot name="mapBox"></slot>
    <div v-show="isUploaded">
      <a class="map-cmd-icon location" @click="getLocation">
        <span class="icon icon-location"></span>
      </a>
      <a class="map-cmd-icon refresh-car" @click="refresh">
        <span class="icon icon-refresh-car"></span>
      </a>
    </div>
  </div>
</template>

<script>
import { onMounted, ref } from "vue";
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);
    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 = () => {
      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);
          }
          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", () => {});
      });
    };
    // 刷新
    const refresh = () => {
      context.emit("refreshCb", map);
    };
    return {
      initMap,
      init,
      checkLoadedTimeoutId,
      getLocation,
      satelActive,
      roadActive,
      refresh,
      isUploaded,
    };
  },
};
</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;
}
.location {
  bottom: 180px;
  right: 20px;
}
.refresh-car {
  bottom: 240px;
  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>