Google autocomplete with apartment number

When a user adds an apartment number to his address, autocomplete seems to look at zipcode and change the proposed matches:

6250 Hollywood Blvd No. 20

Is there a way to make this work, and if not, a way to ignore the apartment number and send it along with submitting the form as a separate value? (bearing in mind that the user can use #, Unit, Apt, etc. to determine their device number)

var placeSearch, autocomplete; var componentForm = { street_number: 'short_name', route: 'short_name', locality: 'long_name', administrative_area_level_1: 'short_name', country: 'long_name', postal_code: 'short_name' }; function initialize(){ autocomplete = new google.maps.places.Autocomplete( (document.getElementById('autocomplete-address')), { types: ['geocode'] }); google.maps.event.addListener(autocomplete, 'place_changed', function(){ fillInAddress(); } ); } function fillInAddress(){ var place = autocomplete.getPlace(); for(var component in componentForm){ document.getElementById(component).value = ''; document.getElementById(component).disabled = false; } for(var i = 0; i < place.address_components.length; i++){ var addressType = place.address_components[i].types[0]; if(componentForm[addressType]){ var val = place.address_components[i][componentForm[addressType]]; document.getElementById(addressType).value = val; } } } 
+6
source share
1 answer

It seems that there is no parameter allowing the Autocomplete API to ignore the apt number, however we can trim the text before passing the string to the API.

in autocomplete = new google.maps.places.Autocomplete( , you can do something like:

 document.getElementById('autocomplete-address').replace(/#.*/g,""); 

since document.getElementById('autocomplete-address') returns a string, we can replace paer #number an empty string using .replace(/#.*/g,"")

+1
source

Source: https://habr.com/ru/post/989831/


All Articles