Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Places Autocomplete suggestions with Unit No/Subpremise is not coming in the response array

I am using Google places API to auto-complete addresses using javascript

When I type the unit number and street number of address in the input box it shows the results in suggestion drop-down but when I select the address the listener for action 'place_changed' event don't have any address with address_component with type 'subpremise' even the formatted_address property don't have the unit number in it.Though it does contain other details from 'street number','city','country' etc

For example : If I type "1403/648 Bourke Street" with a country restriction to Australia. It shows me 5 results in the dropdown with first one as "1403/648 Bourke street, Melbourne,Australia" but when I select this option all I get in the place_change event listener is "648 Bourke street, Melbourne, Australia"

This issue is intermittent though, it works for some unit addresses but fail for other.Any suggestion will be highly appreciated!!

like image 987
Torukmakto Avatar asked Jul 30 '13 00:07

Torukmakto


People also ask

How do I use Google Places autocomplete API?

Ensure that the API key(s) used for all Place Autocomplete and Place Details requests within a session belong to the same Cloud Console project. Be sure to pass a unique session token for each new session. Using the same token for more than one session will result in each request being billed individually.

How would you implement Google Places autocomplete programmatically?

To launch the autocomplete widget using an intent, follow these steps: Use Autocomplete. IntentBuilder to create an intent, passing the desired Autocomplete mode. The intent must call startActivityForResult , passing in a request code that identifies the intent.


1 Answers

It seems to me like Google is validating that the address is real (well atleast real when they last updated their places database - I guess with government records). The problem is that subdivisions are happening all the time where new "subpremise" addresses are created, and a look up on these seems to be br0ken more often. It's strange that they allow a "non-valid" address as an autocomplete suggestion but then limit the result.

A look up on the Sydney address below will return "subpremise" and "street_number"

"9/321 Pitt Street, Sydney, New South Wales, Australia"

Where the Melbourne address below only gets as accurate as "route"

"2/321 Pitt Street, Brunswick, Victoria, Australia"

The biggest problem is that the result doesn't return the unit or street number anywhere in the reply.

I have hacked together the following JS to compare the user-entered address with the returned result. If the "numbers" are missing, they are added. You can customise as you need to fit with your code.

if (addressType == 'route') {     var regex = RegExp('^(.*)'+GPLACESSTREET.split(' ',1)[0]), // get all the user entered values before a match with the first word from the Google result         result = regex.exec(INPUTFEILD.value);      if ( Array.isArray(result) ) {         FULLSTREETADDRESS = result[1]+''+GPLACESSTREET; // add the street name to the user-entered unit & street number     } } 
like image 90
MountainAsh Avatar answered Sep 22 '22 05:09

MountainAsh