Baidu.vue 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <template>
  2. <div ref="wrapRef" :style="{ height, width }"></div>
  3. </template>
  4. <script lang="ts">
  5. import { defineComponent, ref, nextTick, unref, onMounted } from 'vue';
  6. import { useScript } from '/@/hooks/web/useScript';
  7. const BAI_DU_MAP_URL = 'https://api.map.baidu.com/getscript?v=3.0&ak=OaBvYmKX3pjF7YFUFeeBCeGdy9Zp7xB2&services=&t=20210201100830&s=1';
  8. export default defineComponent({
  9. name: 'BaiduMap',
  10. props: {
  11. width: {
  12. type: String,
  13. default: '100%',
  14. },
  15. height: {
  16. type: String,
  17. default: 'calc(100vh - 78px)',
  18. },
  19. },
  20. setup() {
  21. const wrapRef = ref<HTMLDivElement | null>(null);
  22. const { toPromise } = useScript({ src: BAI_DU_MAP_URL });
  23. async function initMap() {
  24. await toPromise();
  25. await nextTick();
  26. const wrapEl = unref(wrapRef);
  27. if (!wrapEl) return;
  28. const BMap = (window as any).BMap;
  29. const map = new BMap.Map(wrapEl);
  30. const point = new BMap.Point(116.404, 39.915);
  31. map.centerAndZoom(point, 15);
  32. map.enableScrollWheelZoom(true);
  33. }
  34. onMounted(() => {
  35. initMap();
  36. });
  37. return { wrapRef };
  38. },
  39. });
  40. </script>