|
| 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