I think this is a new question and I have some example code.
The code for google places autocomplete API is:
function initialize() {
var in
I am not sure what you meant by town and region but you may try the following as i did to get city and state of a selected place
for town you can try : locality
or sublocality_level_1
property.If locality
returns no result use sublocality_level_1
, I suggest try both
and for region you may try : administrative_area_level_1
and for country use : country
Here's the api documentation : place autocomplete api documentation
Update :
Here's what you should do :
1.First you should get the address_component
from response
var place = autocomplete.getPlace().address_components
//it will give you an array of objects which has a type property.This type property holds different components of your address.Loop through it to get your desired component
address_component = [
{
"long_name" : "Australia",
"short_name": "Aus",
"types" : ["country"]
},
{
"long_name" : "Pyrmont",
"short_name" : "Pyrmont",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Council of the City of Sydney",
"short_name" : "Sydney",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New South Wales",
"short_name" : "NSW",
"types" : [ "administrative_area_level_1", "political" ]
}
]
you will loop through and find the types you need and get short_name or long _name and show it to the screen.If you understand this, you are good to go.If not you can ask me .Check all the components available Place Details
CODE :
declare a component map which will house the components you want to use only
var componentMap = {
country: 'country',
locality: 'locality',
administrative_area_level_1 : 'administrative_area_level_1',
};
for(var i = 0; i < place.length; i++){
var types = place[i].types; // get types array of each component
for(var j = 0; j < types.length; j++){ // loop through the types array of each component as types is an array and same thing can be indicated by different name.As you can see in the json object above
var component_type = types[j];
// check if this type is in your component map.If so that means you want this component
if(componentMap.hasOwnProperty(component_type)){
console.log(place[i]['long_name']);
}
}
}