Skip to content

Commit 91f9317

Browse files
committed
🆕 new: add fieldGoogleAddress #33
1 parent 776f6c4 commit 91f9317

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

dev/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<script type="text/javascript" src="https://nosir.github.io/cleave.js/lib/cleave-phone.i18n.js"></script>
3131
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/noUiSlider/8.5.1/nouislider.js"></script>
3232
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pikaday/1.4.0/pikaday.min.js"></script>
33+
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCEz-sX9bRJorDS-D_JL0JkZVZe2gzoUMw&amp;libraries=places"></script>
3334

3435
</head>
3536
<body>

dev/schema.js

+10
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,16 @@ module.exports = {
180180
label: "E-mail (email field)",
181181
model: "email",
182182
placeholder: "User's e-mail address"
183+
}, {
184+
type: "googleAddress",
185+
label: "Location (googleAddress)",
186+
model: "location",
187+
placeholder: "Location",
188+
onPlaceChanged(value, place, rawPlace, model, schema) {
189+
console.log("Location changed! " + value);
190+
//console.log(place);
191+
//console.log(rawPlace);
192+
}
183193
}, {
184194
type: "text",
185195
label: "Phone",

src/fields/fieldGoogleAddress.vue

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<template lang="jade">
2+
input.form-control(type="text", v-model="value", :readonly="schema.readonly", :disabled="disabled", :placeholder="schema.placeholder", debounce="500", @focus="geolocate()")
3+
</template>
4+
5+
<script>
6+
/**
7+
* Based on gocanto"s Google Autocomplete library
8+
* https://github.com/gocanto/google-autocomplete
9+
*/
10+
11+
import abstractField from "./abstractField";
12+
import { isFunction } from "lodash";
13+
14+
/* global google */
15+
export default {
16+
mixins: [ abstractField ],
17+
18+
data: function ()
19+
{
20+
return {
21+
//google autocomplete object
22+
"autocomplete": "",
23+
24+
//google inputs retrieved
25+
"inputs": {
26+
"street_number": "long_name",
27+
route: "long_name",
28+
country: "long_name",
29+
administrative_area_level_1: "long_name",
30+
administrative_area_level_2: "long_name",
31+
locality: "long_name",
32+
postal_code: "short_name"
33+
}
34+
};
35+
},
36+
37+
ready() {
38+
if (window.google && window.google.maps && window.google.maps.places && window.google.maps.places.Autocomplete) {
39+
this.autocomplete = new google.maps.places.Autocomplete(this.$el, {
40+
types: ["geocode"]
41+
});
42+
43+
this.autocomplete.addListener("place_changed", this.pipeAddress);
44+
}
45+
else
46+
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!");
47+
},
48+
49+
methods: {
50+
/**
51+
* Look up places and dispatch an event.
52+
* @return void
53+
*/
54+
pipeAddress: function () {
55+
56+
let place = this.autocomplete.getPlace();
57+
if (place) {
58+
59+
this.value = place.formatted_address;
60+
61+
let data = {};
62+
if (place.address_components !== undefined) {
63+
for (let i = 0; i < place.address_components.length; i++) {
64+
let input = place.address_components[i].types[0];
65+
if (this.inputs[input]) {
66+
data[input] = place.address_components[i][this.inputs[input]];
67+
}
68+
}
69+
70+
}
71+
72+
// Call event in schema
73+
if (isFunction(this.schema.onPlaceChanged))
74+
this.schema.onPlaceChanged(this.value, data, place, this.model, this.schema);
75+
}
76+
},
77+
78+
/**
79+
* Get the user location.
80+
* @return void
81+
*/
82+
geolocate: function(){
83+
84+
if (navigator.geolocation) {
85+
navigator.geolocation.getCurrentPosition((position) => {
86+
87+
let geolocation = {
88+
lat: position.coords.latitude,
89+
lng: position.coords.longitude
90+
};
91+
92+
let circle = new google.maps.Circle({
93+
center: geolocation,
94+
radius: position.coords.accuracy
95+
});
96+
97+
this.autocomplete.setBounds(circle.getBounds());
98+
});
99+
}
100+
}
101+
}
102+
};
103+
</script>
104+
105+
<style lang="sass">
106+
</style>

0 commit comments

Comments
 (0)