Gaode.vue 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 A_MAP_URL = 'https://webapi.amap.com/maps?v=2.0&key=06313eb9c6563b674a8fd789db0692c3';
  8. export default defineComponent({
  9. name: 'AMap',
  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: A_MAP_URL });
  23. async function initMap() {
  24. await toPromise();
  25. await nextTick();
  26. const wrapEl = unref(wrapRef);
  27. if (!wrapEl) return;
  28. const AMap = (window as any).AMap;
  29. new AMap.Map(wrapEl, {
  30. zoom: 11,
  31. center: [116.397428, 39.90923],
  32. viewMode: '3D',
  33. });
  34. }
  35. onMounted(() => {
  36. initMap();
  37. });
  38. return { wrapRef };
  39. },
  40. });
  41. </script>