Google.vue 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 MAP_URL =
  8. 'https://maps.googleapis.com/maps/api/js?key=AIzaSyBQWrGwj4gAzKndcbwD5favT9K0wgty_0&signed_in=true';
  9. export default defineComponent({
  10. name: 'GoogleMap',
  11. props: {
  12. width: {
  13. type: String,
  14. default: '100%',
  15. },
  16. height: {
  17. type: String,
  18. default: 'calc(100vh - 78px)',
  19. },
  20. },
  21. setup() {
  22. const wrapRef = ref<HTMLDivElement | null>(null);
  23. const { toPromise } = useScript({ src: MAP_URL });
  24. async function initMap() {
  25. await toPromise();
  26. await nextTick();
  27. const wrapEl = unref(wrapRef);
  28. if (!wrapEl) return;
  29. const google = (window as any).google;
  30. const latLng = { lat: 116.404, lng: 39.915 };
  31. const map = new google.maps.Map(wrapEl, {
  32. zoom: 4,
  33. center: latLng,
  34. });
  35. new google.maps.Marker({
  36. position: latLng,
  37. map: map,
  38. title: 'Hello World!',
  39. });
  40. }
  41. onMounted(() => {
  42. initMap();
  43. });
  44. return { wrapRef };
  45. },
  46. });
  47. </script>