Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict Google Places Autocomplete to return addresses only

autocomplete = new google.maps.places.Autocomplete(input, { types: ['geocode'] }); 

returns streets and cities amongst other larger areas. Is it possible to restrict to streets only?

like image 647
randomguy Avatar asked Feb 25 '13 16:02

randomguy


People also ask

How do I restrict Google Places API?

Step 1: Ensure you're logged in with the Google account under which you created your Google Maps API key. Step 2: Navigate to Google Console Credentials screen and click to your API key to enter it. Step 3: In API restrictions section choose 'Restrict key' and click on 'Select APIs' dropdown. That's it!

How does Google address autocomplete work?

As the user enters text, autocomplete returns place predictions in the form of a dropdown pick list. When the user selects a place from the list, information about the place is returned to the autocomplete object, and can be retrieved by your application.

What is address autocomplete?

Address Autocomplete is an address form feature that suggests street addresses to users as they type an address into a form. Because an Autocomplete function reduces the number of keystrokes & mistakes that a user types, it can make data submission faster and more accurate.


1 Answers

This question is old, but I figured I'd add to it in case anyone else is having this issue. restricting types to 'address' unfortunately does not achieve the expected result, as routes are still included. Thus, what I decided to do is loop through the result and implement the following check:

result.predictions[i].types.includes('street_address')

Unfortunately, I was surprised to know that my own address was not being included, as it was returning the following types: { types: ['geocode', 'premise'] }

Thus, I decided to start a counter, and any result that includes 'geocode' or 'route' in its types must include at least one other term to be included (whether that be 'street_address' or 'premise' or whatever. Thus, routes are excluded, and anything with a complete address will be included. It's not foolproof, but it works fairly well.

Loop through the result predictions, and implement the following:

if (result.predictions[i].types.includes('street_address')) {     // Results that include 'street_address' should be included     suggestions.push(result.predictions[i]) } else {     // Results that don't include 'street_address' will go through the check     var typeCounter = 0;     if (result.predictions[i].types.includes('geocode')) {         typeCounter++;     }     if (result.predictions[i].types.includes('route')) {         typeCounter++;     }     if (result.predictions[i].types.length > typeCounter) {         suggestions.push(result.predictions[i])     } } 
like image 59
Logic.Analyzer Avatar answered Oct 04 '22 02:10

Logic.Analyzer