jqueryautocompletegoogle-places-apigoogle-places

google places autocomplete suggestion trigger after X characters


I am attempting to limit the display of suggestions for the Google Place Autocomplete API.

Since the Google API charges a API Call per character entered in the field, I would like to restrict the suggestions to only be shown after X amount of characters have been entered.

My initialization of the autocomplete is as standard as it gets.

 var input = document.getElementbyId('Searchbox');
 var autocomplete = new google.maps.places.autocomplete(input)

Any tips or suggestions would be greatly appreciated

Thanks


Solution

  • You can read the length of a string in javascript using length.

    var input = document.getElementById("Searchbox");
    if(input.length > 3)
    {
        var autocomplete = new google.maps.places.autocomplete(input);
    }
    

    In this example, the autocomplete-function will only be fired if the input has length 4 or higher.

    Also, I recommend that you cache results for as long as you like, to avoid useless charges.