forked from vue-generators/vue-form-generator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfieldGoogleAddress.vue
107 lines (94 loc) · 2.62 KB
/
fieldGoogleAddress.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<template lang="pug">
input.form-control(type="text", v-model="value", :autocomplete="fieldOptions.autocomplete", :disabled="disabled", :placeholder="placeholder", :readonly="readonly", :name="inputName", debounce="500", @focus="geolocate()", :id="fieldID")
</template>
<script>
/**
* Based on gocanto"s Google Autocomplete library
* https://github.com/gocanto/google-autocomplete
*/
import abstractField from "../abstractField";
import { isFunction } from "lodash";
/* global google */
export default {
name: "field-googleAddress",
mixins: [abstractField],
data() {
return {
// google autocomplete object
autocomplete: "",
// google inputs retrieved
inputs: {
street_number: "long_name",
route: "long_name",
country: "long_name",
administrative_area_level_1: "long_name",
administrative_area_level_2: "long_name",
locality: "long_name",
postal_code: "short_name"
}
};
},
mounted() {
this.$nextTick(() => {
if (
window.google &&
window.google.maps &&
window.google.maps.places &&
window.google.maps.places.Autocomplete
) {
this.autocomplete = new google.maps.places.Autocomplete(this.$el, {
types: ["geocode"]
});
this.autocomplete.addListener("place_changed", this.pipeAddress);
} else {
console.warn(
"Google Maps API is missing. Please add https://maps.googleapis.com/maps/api/js?key=YOUR_KEY&libraries=places script in the HTML head section!"
);
}
});
},
methods: {
/**
* Look up places and dispatch an event.
* @return void
*/
pipeAddress() {
let place = this.autocomplete.getPlace();
if (place) {
this.value = place.formatted_address;
let data = {};
if (place.address_components !== undefined) {
for (let i = 0; i < place.address_components.length; i++) {
let input = place.address_components[i].types[0];
if (this.inputs[input]) {
data[input] = place.address_components[i][this.inputs[input]];
}
}
}
// Call event in schema
if (isFunction(this.fieldOptions.onPlaceChanged))
this.fieldOptions.onPlaceChanged(this.value, data, place, this.model, this.schema);
}
},
/**
* Get the user location.
* @return void
*/
geolocate() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
let geolocation = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
let circle = new window.google.maps.Circle({
center: geolocation,
radius: position.coords.accuracy
});
this.autocomplete.setBounds(circle.getBounds());
});
}
}
}
};
</script>