Address.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <template>
  2. <div>
  3. <v-autocomplete
  4. v-model="model"
  5. :loading="loading"
  6. :items="items"
  7. :search-input.sync="search"
  8. hide-no-data
  9. hide-details
  10. return-object
  11. auto-select-first
  12. clearable
  13. :label="$t('where') + ' ?'"
  14. outlined
  15. append-icon="mdi-crosshairs-gps"
  16. @change="$emit('change', $event ? $event.value : '')"
  17. @click:append="geolocalizeUser"
  18. />
  19. <v-snackbar :value="errorMsg !== ''">
  20. {{ errorMsg }}
  21. <template #action="{ attrs }">
  22. <v-btn text v-bind="attrs" @click="errorMsg=''">
  23. {{ $t('close') }}
  24. </v-btn>
  25. </template>
  26. </v-snackbar>
  27. </div>
  28. </template>
  29. <script>
  30. export default {
  31. data () {
  32. return {
  33. model: null,
  34. search: null,
  35. features: [],
  36. loading: false,
  37. errorMsg: ''
  38. }
  39. },
  40. computed: {
  41. items () {
  42. return this.features.map((f) => {
  43. return {
  44. text: f.properties.name + ' (' + f.properties.postcode + ')',
  45. value: { longitude: f.geometry.coordinates[0], latitude: f.geometry.coordinates[1] },
  46. disabled: false
  47. }
  48. })
  49. }
  50. },
  51. watch: {
  52. search (val) {
  53. if (!val) {
  54. this.features = []
  55. return
  56. }
  57. this.loading = true
  58. // Lazily load input items
  59. fetch('https://api-adresse.data.gouv.fr/search/?type=municipality&autocomplete=1&limit=5&q=' + val)
  60. .then(res => res.json())
  61. .then(({ features }) => {
  62. this.features = features
  63. })
  64. .catch((err) => {
  65. // eslint-disable-next-line no-console
  66. console.error(err)
  67. })
  68. .finally(() => {
  69. this.loading = false
  70. })
  71. }
  72. },
  73. methods: {
  74. geolocalizeUser () {
  75. if (navigator.geolocation) {
  76. navigator.geolocation.getCurrentPosition(
  77. (position) => {
  78. this.latitude = position.coords.latitude
  79. this.longitude = position.coords.longitude
  80. this.text = this.$t('my_position')
  81. },
  82. () => {
  83. this.errorMsg = this.$t('geolocation_error')
  84. }
  85. )
  86. } else {
  87. this.errorMsg = this.$t('geolocation_not_supported')
  88. }
  89. }
  90. }
  91. }
  92. </script>
  93. <style>
  94. .v-input__control {
  95. height: 56px;
  96. }
  97. </style>