-
Notifications
You must be signed in to change notification settings - Fork 532
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<template lang="jade"> | ||
input.form-control(type="text", v-model="value", :readonly="schema.readonly", :disabled="disabled", :placeholder="schema.placeholder", debounce="500", @focus="geolocate()") | ||
</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 { | ||
mixins: [ abstractField ], | ||
data: function () | ||
{ | ||
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" | ||
} | ||
}; | ||
}, | ||
ready() { | ||
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: function () { | ||
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.schema.onPlaceChanged)) | ||
this.schema.onPlaceChanged(this.value, data, place, this.model, this.schema); | ||
} | ||
}, | ||
/** | ||
* Get the user location. | ||
* @return void | ||
*/ | ||
geolocate: function(){ | ||
if (navigator.geolocation) { | ||
navigator.geolocation.getCurrentPosition((position) => { | ||
let geolocation = { | ||
lat: position.coords.latitude, | ||
lng: position.coords.longitude | ||
}; | ||
let circle = new google.maps.Circle({ | ||
center: geolocation, | ||
radius: position.coords.accuracy | ||
}); | ||
this.autocomplete.setBounds(circle.getBounds()); | ||
}); | ||
} | ||
} | ||
} | ||
}; | ||
</script> | ||
|
||
<style lang="sass"> | ||
</style> |