Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add session token on Google Place AutoComplete API

I have an input for address searching that call the google API for autocomplete. It works but I want to add a session token on the query but I don't know how to do that.

I search everywhere on the internet but I only see: "use var sessionToken = new google.maps.places.AutocompleteSessionToken(); for generating a session token". Ok but I don't know where to put this variable :/

<script type="text/javascript"  src="https://maps.googleapis.com/maps/api/js?libraries=places&key=MY_API_KEY"></script>
<form>
    <label>Address</label>
    <input id="user_input_autocomplete_address" placeholder="Votre adresse...">
</form>
<script>
    function initializeAutocomplete(id) {
        var element = document.getElementById(id);

        if (element) {
            var autocomplete = new google.maps.places.Autocomplete(element, { types: ['address'] });
            autocomplete.setComponentRestrictions({'country': ['fr']});
            google.maps.event.addListener(autocomplete, 'place_changed', onPlaceChanged);
        }
    }   
    function onPlaceChanged() {  
        var place = this.getPlace();
        console.log(place);

        for (var i in place.address_components) {    
            var component = place.address_components[i];    

            for (var j in component.types) {
                var type_element = document.getElementById(component.types[j]);      

                if (type_element) {        
                    type_element.value = component.long_name;
                }    
            }  
        }
    }
    google.maps.event.addDomListener(window, 'load', function() {  
        initializeAutocomplete('user_input_autocomplete_address');
    });
</script>

It's working, I have the auto completion but this script doesn't use a unique Session Token. So for each letter typed in the input, each query use a different query.

like image 545
Launch IT Avatar asked Jul 09 '19 08:07

Launch IT


People also ask

What is session token in Google Places API?

Place Autocomplete uses session tokens to 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 and a call to Place Details is made.

What is session token in API?

Session Token API endpoint URL. /session. Creates a session token (referred to as an User API Access Token in the UI) that provides authentication for other API calls. Note: You can't use a session token for authenticating a /datapoint , /backfill , or /event API call.


1 Answers

You're using the Autocomplete widget which handles sessions automatically. You don't need to generate sessions yourself according to Google's documentation.

If you wanted to use the AutocompleteService class though, yes you would need to use google.maps.places.AutocompleteSessionToken(). See Google's code example in the link above to see how/where it's added:

// Create a new session token.
var sessionToken = new google.maps.places.AutocompleteSessionToken();

// Pass the token to the autocomplete service.
var autocompleteService = new google.maps.places.AutocompleteService();
autocompleteService.getPlacePredictions({
  input: 'pizza near Syd',
  sessionToken: sessionToken
},
displaySuggestions);

Hope this helps!

like image 72
evan Avatar answered Sep 27 '22 15:09

evan