123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <template>
- <div>
- <label v-if="showLocationLabel">Location</label>
- <div v-if="showAutoComplete" class="input-group-prepend">
- <span class="input-group-text">
- <eva-icon name="map" fill="#24aae1"></eva-icon>
- </span>
- <gmap-autocomplete class="form-control" @place_changed="setPlace"></gmap-autocomplete>
- <button class="btn btn-primary btn-l" @click="addMarker">Add</button>
- <br />
- </div>
- <br v-if="showAutoComplete" />
- <gmap-map v-if="showMap" :center="coordinates" :zoom="18" style="width:100%; height: 400px;">
- <gmap-marker
- :key="index"
- v-for="(m, index) in markers"
- :position="m.position"
- :draggable="true"
- @click="center = m.position"
- ></gmap-marker>
- </gmap-map>
- <br v-if="showMap" />
- </div>
- </template>
-
- <script>
- export default {
- name: "MapLocation",
- props: {
- showMap: {
- default: true,
- },
- showLocationLabel: {
- default: true,
- },
- showAutoComplete: {
- default: true,
- },
- setLocation: {
- default: {},
- },
- },
- data() {
- return {
- coordinates: {
- lat: 0,
- lng: 0,
- },
- center: { lat: 0, lng: 0 },
- markers: [],
- places: [],
- currentPlace: {},
- streetNumber: "",
- streetName: "",
- suburb: "",
- city: "",
- province: "",
- country: "",
- postalCode: "",
- url: "",
- };
- },
- created() {
- this.$getLocation({})
- .then((coord) => {
- this.coordinates = coord;
- //this.markers.push({ position: coord })
- })
- .catch((error) => alert(error));
- },
-
- mounted() {
- this.geolocate();
- },
-
- methods: {
- setPlace(place) {
- this.currentPlace = place;
-
- this.streetNumber = "";
- this.streetName = "";
- this.suburb = "";
- this.city = "";
- this.province = "";
- this.country = "";
- this.postalCode = "";
- this.url = "";
-
- for (let i = 0; i < place.address_components.length; i++) {
- if (place.address_components[i].types[0] === "street_number") {
- this.streetNumber = place.address_components[i].long_name;
- }
- if (place.address_components[i].types[0] === "route") {
- this.streetName = place.address_components[i].long_name;
- }
- if (place.address_components[i].types[0] === "sublocality_level_1") {
- this.suburb = place.address_components[i].long_name;
- }
- if (place.address_components[i].types[0] === "locality") {
- this.city = place.address_components[i].long_name;
- }
- if (
- place.address_components[i].types[0] === "administrative_area_level_1"
- ) {
- this.province = place.address_components[i].long_name;
- }
- if (place.address_components[i].types[0] === "country") {
- this.country = place.address_components[i].long_name;
- }
- if (place.address_components[i].types[0] === "postal_code") {
- this.postalCode = place.address_components[i].long_name;
- }
- this.url = place.url;
- }
- },
- addMarker() {
- if (this.currentPlace) {
- const marker = {
- lat: this.currentPlace.geometry.location.lat(),
- lng: this.currentPlace.geometry.location.lng(),
- };
- this.markers = [];
- this.places = [];
- this.markers.push({ position: marker });
- this.places.push(this.currentPlace);
- this.center = marker;
- this.coordinates = marker;
- this.currentPlace = null;
- this.$emit("GoogleAddress", {
- streetNumber: this.streetNumber,
- streetName: this.streetName,
- suburb: this.suburb,
- city: this.city,
- province: this.province,
- country: this.country,
- postalCode: this.postalCode,
- url: this.url,
- coordinates: marker,
- });
- }
- },
- geolocate: function () {
- navigator.geolocation.getCurrentPosition((position) => {
- this.center = {
- lat: position.coords.latitude,
- lng: position.coords.longitude,
- };
- });
- },
- },
- watch: {
- setLocation: {
- immediate: true,
- handler(val, oldVal) {
- if (val && val.lat) {
- this.coordinates = val;
- this.markers = [];
- this.markers.push({ position: val });
- }
- },
- },
- },
- };
- </script>
|