Skip to content

Commit d035e34

Browse files
authored
Added the option to specify which fields to fetch in the PlaceResult object (#103)
* Added the option to specify which fields to fetch * Updated docs
1 parent c688686 commit d035e34

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ I have tried to use different Vue Google Autocomplete components, but did not fi
1313
* Load more than one autocompletion inputs (I could not achieve this with existing vue components)
1414
* Getting geolocation data (latitude, longitude) for found address object along with other address data (country, city, state, county, street, house number, zip code). So no need to do additional geocode request on backend side.
1515
* No external dependencies
16-
* You can get access to underlying [PlaceResult object](https://developers.google.com/maps/documentation/javascript/reference#PlaceResult) to get more details about found location.
16+
* You can get access to underlying [PlaceResult object](https://developers.google.com/maps/documentation/javascript/reference#PlaceResult) to get more details about found location. You are able to specify the specific fields you want to fetch from the PlaceResult object.
1717
* You can limit results to specific country or use users geolocation data
1818

1919
## Installation
@@ -93,6 +93,15 @@ Types supported in place autocomplete requests. [More info](https://developers.g
9393

9494
You may find [this example](#correct-usage-of-the-types-parameter) helpful.
9595

96+
97+
#### fields
98+
Type: `Array`
99+
Default: `['address_components', 'adr_address', 'alt_id', 'formatted_address', 'geometry', 'icon', 'id', 'name', 'permanently_closed', 'photo', 'place_id', 'scope', 'type', 'url', 'utc_offset', 'vicinity']`
100+
101+
Set which data fields to return in the PlaceResult from the Google Autocomplete API when the user selects a place. [Google Autocomplete API by default returns all available data fields](https://developers.google.com/maps/documentation/javascript/places-autocomplete#get_place_information) for the selected place, which may result in additional charges and thus the API users might pay for data they don't need. This package sets a sensible default for the fields value, fetching only the Basic Data fields which do not result in any additional charges. If you want to fetch other fields in addition to the default ones, make sure that the array you pass in to the `fields` prop contains the default fields listed above, and not only the additional fields you want to fetch.
102+
103+
Refer to [this page](https://developers.google.com/maps/billing/understanding-cost-of-use#data-skus) for more details on how certain data fields are billed.
104+
96105
#### country
97106
Type: `String`|`Array`
98107
Default: null

src/VueGoogleAutocomplete.vue

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@
3131
const REGIONS_TYPE = ['locality', 'sublocality', 'postal_code', 'country',
3232
'administrative_area_level_1', 'administrative_area_level_2'];
3333
34+
/*
35+
By default, we're only including basic place data because requesting these
36+
fields place data is not additionally charged by Google. Please refer to:
37+
38+
https://developers.google.com/maps/billing/understanding-cost-of-use#basic-data
39+
*/
40+
const BASIC_DATA_FIELDS = ['address_components', 'adr_address', 'alt_id',
41+
'formatted_address', 'geometry', 'icon', 'id', 'name',
42+
'permanently_closed', 'photo', 'place_id', 'scope', 'type', 'url',
43+
'utc_offset', 'vicinity'];
44+
3445
export default {
3546
name: 'VueGoogleAutocomplete',
3647
@@ -57,6 +68,13 @@
5768
default: 'address'
5869
},
5970
71+
fields: {
72+
type: Array,
73+
default: function() {
74+
return BASIC_DATA_FIELDS;
75+
},
76+
},
77+
6078
country: {
6179
type: [String, Array],
6280
default: null
@@ -143,6 +161,8 @@
143161
options
144162
);
145163
164+
this.autocomplete.setFields(this.fields);
165+
146166
this.autocomplete.addListener('place_changed', this.onPlaceChanged);
147167
},
148168

0 commit comments

Comments
 (0)