Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

mapSetLocation.vue 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <template>
  2. <div>
  3. <label v-if="showLocationLabel">Location</label>
  4. <div v-if="showAutoComplete" class="input-group-prepend">
  5. <span class="input-group-text">
  6. <eva-icon name="map" fill="#24aae1"></eva-icon>
  7. </span>
  8. <gmap-autocomplete class="form-control" @place_changed="setPlace"></gmap-autocomplete>
  9. <button class="btn btn-primary btn-l" @click="addMarker">Add</button>
  10. <br />
  11. </div>
  12. <br v-if="showAutoComplete" />
  13. <gmap-map v-if="showMap" :center="coordinates" :zoom="18" style="width:100%; height: 400px;">
  14. <gmap-marker
  15. :key="index"
  16. v-for="(m, index) in markers"
  17. :position="m.position"
  18. :draggable="true"
  19. @click="center = m.position"
  20. ></gmap-marker>
  21. </gmap-map>
  22. <br v-if="showMap" />
  23. </div>
  24. </template>
  25. <script>
  26. export default {
  27. name: "MapLocation",
  28. props: {
  29. showMap: {
  30. default: true,
  31. },
  32. showLocationLabel: {
  33. default: true,
  34. },
  35. showAutoComplete: {
  36. default: true,
  37. },
  38. setLocation: {
  39. default: {},
  40. },
  41. },
  42. data() {
  43. return {
  44. coordinates: {
  45. lat: 0,
  46. lng: 0,
  47. },
  48. center: { lat: 0, lng: 0 },
  49. markers: [],
  50. places: [],
  51. currentPlace: {},
  52. streetNumber: "",
  53. streetName: "",
  54. suburb: "",
  55. city: "",
  56. province: "",
  57. country: "",
  58. postalCode: "",
  59. url: "",
  60. };
  61. },
  62. created() {
  63. this.$getLocation({})
  64. .then((coord) => {
  65. this.coordinates = coord;
  66. //this.markers.push({ position: coord })
  67. })
  68. .catch((error) => alert(error));
  69. },
  70. mounted() {
  71. this.geolocate();
  72. },
  73. methods: {
  74. setPlace(place) {
  75. this.currentPlace = place;
  76. this.streetNumber = "";
  77. this.streetName = "";
  78. this.suburb = "";
  79. this.city = "";
  80. this.province = "";
  81. this.country = "";
  82. this.postalCode = "";
  83. this.url = "";
  84. for (let i = 0; i < place.address_components.length; i++) {
  85. if (place.address_components[i].types[0] === "street_number") {
  86. this.streetNumber = place.address_components[i].long_name;
  87. }
  88. if (place.address_components[i].types[0] === "route") {
  89. this.streetName = place.address_components[i].long_name;
  90. }
  91. if (place.address_components[i].types[0] === "sublocality_level_1") {
  92. this.suburb = place.address_components[i].long_name;
  93. }
  94. if (place.address_components[i].types[0] === "locality") {
  95. this.city = place.address_components[i].long_name;
  96. }
  97. if (
  98. place.address_components[i].types[0] === "administrative_area_level_1"
  99. ) {
  100. this.province = place.address_components[i].long_name;
  101. }
  102. if (place.address_components[i].types[0] === "country") {
  103. this.country = place.address_components[i].long_name;
  104. }
  105. if (place.address_components[i].types[0] === "postal_code") {
  106. this.postalCode = place.address_components[i].long_name;
  107. }
  108. this.url = place.url;
  109. }
  110. },
  111. addMarker() {
  112. if (this.currentPlace) {
  113. const marker = {
  114. lat: this.currentPlace.geometry.location.lat(),
  115. lng: this.currentPlace.geometry.location.lng(),
  116. };
  117. this.markers = [];
  118. this.places = [];
  119. this.markers.push({ position: marker });
  120. this.places.push(this.currentPlace);
  121. this.center = marker;
  122. this.coordinates = marker;
  123. this.currentPlace = null;
  124. this.$emit("GoogleAddress", {
  125. streetNumber: this.streetNumber,
  126. streetName: this.streetName,
  127. suburb: this.suburb,
  128. city: this.city,
  129. province: this.province,
  130. country: this.country,
  131. postalCode: this.postalCode,
  132. url: this.url,
  133. coordinates: marker,
  134. });
  135. }
  136. },
  137. geolocate: function () {
  138. navigator.geolocation.getCurrentPosition((position) => {
  139. this.center = {
  140. lat: position.coords.latitude,
  141. lng: position.coords.longitude,
  142. };
  143. });
  144. },
  145. },
  146. watch: {
  147. setLocation: {
  148. immediate: true,
  149. handler(val, oldVal) {
  150. if (val && val.lat) {
  151. this.coordinates = val;
  152. this.markers = [];
  153. this.markers.push({ position: val });
  154. }
  155. },
  156. },
  157. },
  158. };
  159. </script>