skynet2 / ngx-google-places-autocomplete

Google Places autocomplete for angular web project
MIT License
91 stars 76 forks source link

How to pass a sessionToken to my request? #75

Open jaradehr1 opened 3 years ago

jaradehr1 commented 3 years ago

The Per Request is the default billing option when you use AutocompleteService(). Charges are applied per keystroke, that could lead to higher billings. I am trying to use Session Tokens when implementing AutocompleteService.getPlacePredictions() to group together autocomplete requests for billing purposes.

Session tokens group the query and selection phases of a user autocomplete search into a discrete session for billing purposes. The session begins when the user starts typing a query, and concludes when they select a place. Each session can have multiple queries, followed by one place selection.

Please see the JS code

// Create a new session token.
let sessionToken = new google.maps.places.AutocompleteSessionToken();
// Pass the token to the autocomplete service.
let autocompleteService = new google.maps.places.AutocompleteService();
autocompleteService.getPlacePredictions({
  input: user_input,
  sessionToken: sessionToken
}, suggestions);

The handleAddressChange(address: Address) fires on selection, but there is no other event that fires per keystroke

Any help how to do this in my Angular 8? Please, refer to my stackoverflow question

tobika commented 3 years ago

It would be great to know if this is an issue with this plugin. If we could all safe some money I'd also be ready to do a PR :-)

tobika commented 3 years ago

https://developers.google.com/maps/documentation/javascript/places-autocomplete

It seems that sessionToken is only used with the getPlacePredictions and not getPlace that is used in this project.

IslombekHasan commented 3 years ago

Some more insight on this. Since this plugin uses an Autocomplete widget, session tokens are automatically handled in the background. Please refer to this link. https://developers.google.com/maps/documentation/javascript/places-autocomplete#session_tokens

jaradehr1 commented 3 years ago

Some more insight on this. Since this plugin uses an Autocomplete widget, session tokens are automatically handled in the background. Please refer to this link. https://developers.google.com/maps/documentation/javascript/places-autocomplete#session_tokens

Correct, they are automatically handled. However, the autocomplete widget does not give you control over how to use that session which leads to a higher bill.

jaradehr1 commented 3 years ago

If I can get your input on this guys:

https://stackoverflow.com/questions/64864286/how-to-implement-session-tokens-properly-with-google-autocomplete-service-api-in

IslombekHasan commented 3 years ago

Some more insight on this. Since this plugin uses an Autocomplete widget, session tokens are automatically handled in the background. Please refer to this link. https://developers.google.com/maps/documentation/javascript/places-autocomplete#session_tokens

Correct, they are automatically handled. However, the autocomplete widget does not give you control over how to use that session which leads to a higher bill.

Indeed. Though, I wonder, do we need that control? If you test the widget and have a look at the Network tab, you'll see that AutocompletionService.GetPredictions method is fired together with a query that has a parameter&20s. Right after this comes a token, which is reused in all other GetPredictions requests until you fire a GetPlaceDetails by choosing a place. I checked this several times and it was a relief to know that.

jaradehr1 commented 3 years ago

Some more insight on this. Since this plugin uses an Autocomplete widget, session tokens are automatically handled in the background. Please refer to this link. https://developers.google.com/maps/documentation/javascript/places-autocomplete#session_tokens

Correct, they are automatically handled. However, the autocomplete widget does not give you control over how to use that session which leads to a higher bill.

Indeed. Though, I wonder, do we need that control? If you test the widget and have a look at the Network tab, you'll see that AutocompletionService.GetPredictions method is fired together with a query that has a parameter&20s. Right after this comes a token, which is reused in all other GetPredictions requests until you fire a GetPlaceDetails by choosing a place. I checked this several times and it was a relief to know that.

hmm.. I think I read in the documentation that every key stroke is considered an api call with (different session token). That is what the implementation of Autocomplete widget that uses getPlace function.. with get predication, you get to choose to use the same token over and over with every key stroke until the user selects a place. Correct me if I am wrong.