Skip to content
This repository was archived by the owner on Sep 24, 2024. It is now read-only.

Commit fe7c80c

Browse files
olefirenkobemyguestdmitriy
authored andcommitted
Option to restrict the autocomplete search to a particular country
1 parent ed69c35 commit fe7c80c

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,13 @@ The input field will get this placeholder text.
8888
Type: `String`
8989
Default: `address`
9090

91-
Types supported in place autocomplete requests. [More info] (https://developers.google.com/places/supported_types#table3)
91+
Types supported in place autocomplete requests. [More info](https://developers.google.com/places/supported_types#table3)
92+
93+
#### country
94+
Type: `String`
95+
Default: null
96+
97+
Option to restrict the autocomplete search to a particular country. Countries must be passed as as a two-character, ISO 3166-1 Alpha-2 compatible country code (i.e. "br", "sg", "fr").
9298

9399

94100
#### enable-geolocation
@@ -113,6 +119,7 @@ Please note that you need to provide what method will listen (`v-on:placechanged
113119
classname="form-control"
114120
placeholder="Please type your address"
115121
v-on:placechanged="getAddressData"
122+
country="sg"
116123
>
117124
</vue-google-autocomplete>
118125
</div>

example/src/index.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ <h2 class="subtitle">A Vue.js autosuggest component for the Google Maps Places A
2020
<div class="container" id="app">
2121
<h3 class="title is-4">Start typing an address and below you will see found result</h3>
2222
<p class="control">
23-
<vue-google-autocomplete
24-
id="map"
23+
<vue-google-autocomplete
24+
id="map"
2525
classname="input"
2626
placeholder="Start typing"
2727
v-on:placechanged="getAddressData"
28+
country="sg"
2829
>
2930
</vue-google-autocomplete>
3031
</p>
@@ -48,4 +49,4 @@ <h3 class="title is-4">Start typing an address and below you will see found resu
4849
<script src="../dist/app.js"></script>
4950

5051
</body>
51-
</html>
52+
</html>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-google-autocomplete",
3-
"version": "1.0.6",
3+
"version": "1.0.7",
44
"description": "Vue component to Google Places Autocomplete",
55
"main": "src/VueGoogleAutocomplete.vue",
66
"homepage": "https://github.com/olefirenko/vue-google-autocomplete",

src/VueGoogleAutocomplete.vue

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
default: 'address'
3030
},
3131
32+
country: {
33+
type: String,
34+
default: null
35+
},
36+
3237
enableGeolocation: {
3338
type: Boolean,
3439
default: false
@@ -48,12 +53,22 @@
4853
},
4954
5055
mounted: function() {
51-
this.autocomplete = new google.maps.places.Autocomplete(
56+
const options = {
57+
types: [this.types]
58+
};
59+
60+
if (this.country) {
61+
options.componentRestrictions = {
62+
country: this.country
63+
};
64+
}
65+
66+
this.autocomplete = new google.maps.places.Autocomplete(
5267
document.getElementById(this.id),
53-
{ types: [this.types] }
68+
options
5469
);
5570
56-
this.autocomplete.addListener('place_changed', () => {
71+
this.autocomplete.addListener('place_changed', () => {
5772
5873
let place = this.autocomplete.getPlace();
5974

0 commit comments

Comments
 (0)