5

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

cfair22
  • 53
  • 1
  • 4

1 Answers1

-1

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.

Sainan
  • 1,274
  • 2
  • 19
  • 32
  • 2
    Thanks!! just enough information to get me started. – cfair22 Oct 12 '16 at 18:43
  • 3
    I'm sorry I downvoted the selected answer. Solution described sets the initialization of the Autocomplete feature once at least 3 chars have been entered. But subsequently, the Autocompletion will happen on every keystroke within the input, no matter how many chars are actually entered. – ffrey Jan 24 '17 at 15:25
  • @ffrey for it, you must implement the code into a function that fires each time the user hit a key. – Néstor Aug 03 '17 at 22:43