Map.vue 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <!--
  2. Leaflet map
  3. @see https://leafletjs.com/
  4. -->
  5. <template>
  6. <div id="map-wrap">
  7. <client-only>
  8. <l-map :zoom="zoom" :center="center">
  9. <l-tile-layer :url="layerUrl" />
  10. <l-marker
  11. :lat-lng="latLong"
  12. draggable
  13. @update:latLng="onMoveMarker"
  14. />
  15. </l-map>
  16. <v-btn class="mr-4 mt-2 mb-2 ot_green ot_white--text" @click="updateMap">
  17. {{ $t('updateMap') }}
  18. </v-btn>
  19. </client-only>
  20. </div>
  21. </template>
  22. <script lang="ts">
  23. import {computed, ComputedRef, defineComponent, ref, Ref, toRefs, ToRefs, useContext} from '@nuxtjs/composition-api'
  24. import {QUERY_TYPE, TYPE_ALERT} from '~/types/enums'
  25. import {AddressPostal} from '~/models/Core/AddressPostal'
  26. import {AnyJson} from '~/types/interfaces'
  27. import Page from "~/services/store/page";
  28. export default defineComponent({
  29. props: {
  30. address: {
  31. type: Object as () => AddressPostal,
  32. required: true
  33. },
  34. zoom: {
  35. type: Number,
  36. required: true
  37. }
  38. },
  39. setup (props, { emit }) {
  40. const { $dataProvider, store, app: {i18n} } = useContext()
  41. const { address }: ToRefs = toRefs(props)
  42. const latitude: Ref<number> = ref(address.value.latitude ?? 0.0)
  43. const longitude: Ref<number> = ref(address.value.longitude ?? 0.0)
  44. const center: ComputedRef<Array<number>> = computed(() => [latitude.value, longitude.value])
  45. const latLong: ComputedRef<Array<number>> = computed(() => [latitude.value, longitude.value])
  46. const layerUrl: string = 'https://{s}.tile.osm.org/{z}/{x}/{y}.png'
  47. const updateMap = async () => {
  48. const response = await $dataProvider.invoke({
  49. type: QUERY_TYPE.DEFAULT,
  50. url: `/api/gps-coordinate-searching?street=${address.value.streetAddress}&cp=${address.value.postalCode}&city=${address.value.addressCity}`
  51. })
  52. const data = response.data
  53. if (data.length > 0) {
  54. latitude.value = data[0].latitude
  55. longitude.value = data[0].longitude
  56. address.value.latitude = data[0].latitude
  57. address.value.longitude = data[0].longitude
  58. emit('updateAddress', address.value)
  59. } else {
  60. new Page(store).addAlerts(TYPE_ALERT.ALERT, [i18n.t('no_coordinate_corresponding') as string])
  61. }
  62. }
  63. const onMoveMarker = async (event: AnyJson) => {
  64. if(event){
  65. address.value.latitude = event.lat
  66. address.value.longitude = event.lng
  67. emit('updateAddress', address.value)
  68. }
  69. }
  70. return {
  71. updateMap,
  72. center,
  73. latLong,
  74. layerUrl,
  75. onMoveMarker
  76. }
  77. }
  78. })
  79. </script>
  80. <style scoped>
  81. #map-wrap{
  82. height: 30vh;
  83. }
  84. </style>