I am using google geocode map api to get the address detail.I have a separate HTML input field for address, address line 2, zip code, city and state in my form. Now,I would like to fetch the address details from the geocode map api response and then auto populate address, address line 2, zip code, city and state value in the respective input field based on address_component keys of API response.

Sample API response:
{
    address_components: [
    {
            long_name: "277",
            short_name: "277",
            types: ["street_number"]
    },
    {
            long_name: "Bedford Avenue",
            short_name: "Bedford Ave",
            types: ["route"]
    },
    {
            long_name: "Williamsburg",
            short_name: "Williamsburg",
            types: ["neighborhood", "political"]
    },
    {
            long_name: "Brooklyn",
            short_name: "Brooklyn",
            types: ["political", "sublocality",
                    "sublocality_level_1"
            ]
    },
    {
            long_name: "Kings County",
            short_name: "Kings County",
            types: ["administrative_area_level_2",
                    "political"
            ]
    },
    {
            long_name: "New York",
            short_name: "NY",
            types: ["administrative_area_level_1",
                    "political"
            ]
    },
    {
            long_name: "United States",
            short_name: "US",
            types: ["country", "political"]
    },
    {
            long_name: "11211",
            short_name: "11211",
            types: ["postal_code"]
    }],
    formatted_address: "277 Bedford Ave, Brooklyn, NY 11211, USA"
 }

So currently, I am using below mapping for US addresses and it is working fine for US.

ADDRESS: ADDRESS_COMPONENT KEYS - short_name - 'street_number', 'route', 'floor'

ADDRESS LINE 2: ADDRESS_COMPONENT KEYS - short_name - 'room', 'premise', 'subpremise', 'landmark'

ZIP / POSTAL CODE: ADDRESS_COMPONENT KEYS - short_name - 'postal_code'

CITY: ADDRESS_COMPONENT KEYS - short_name - 'postal_town', 'locality', 'sublocality'

STATE: ADDRESS_COMPONENT KEYS - short_name - 'administrative_area_level_1'

But when I am trying to use the same logic for International addresses and I am getting different keys for different countries.

Example of some of the International Geocode responses:

ADDRESS1:

{
    address_components: [
    {
        long_name: "810",
        short_name: "810",
        types: ["street_number"]
    },
    {
        long_name: "Dubai Fountain Street",
        short_name: "Dubai Fountain St",
        types: ["route"]
    },
    {
        long_name: "Downtown Dubai",
        short_name: "Downtown Dubai",
        types: ["political", "sublocality", "sublocality_level_1"]
    },
    {
        long_name: "Dubai",
        short_name: "Dubai",
        types: ["locality", "political"]
    },
    {
        long_name: "Dubai",
        short_name: "Dubai",
        types: ["administrative_area_level_1", "political"]
    },
    {
        long_name: "United Arab Emirates",
        short_name: "AE",
        types: ["country", "political"]
    }],
    formatted_address: "810 Dubai Fountain St - Downtown Dubai - Dubai - United Arab Emirates"
}

ADDRESS 2:
{
    address_components: [
    {
            long_name: "Nakase",
            short_name: "Nakase",
            types: ["political", "sublocality",
                    "sublocality_level_2"
            ]
    },
    {
            long_name: "Mihama Ward",
            short_name: "Mihama Ward",
            types: ["political", "sublocality",
                    "sublocality_level_1"
            ]
    },
    {
            long_name: "Chiba",
            short_name: "Chiba",
            types: ["locality", "political"]
    },
    {
            long_name: "Chiba",
            short_name: "Chiba",
            types: ["administrative_area_level_1",
                    "political"
            ]
    },
    {
            long_name: "Japan",
            short_name: "JP",
            types: ["country", "political"]
    },
    {
            long_name: "261-0023",
            short_name: "261-0023",
            types: ["postal_code"]
    }],
    formatted_address: "Nakase, Mihama Ward, Chiba, 261-0023, Japan"
 }



 ADDRESS 3:
 {
    address_components: [
    {
            long_name: "#####",
            short_name: "#####",
            types: ["plus_code"]
    },
    {
            long_name: "Hangzhou",
            short_name: "Hangzhou",
            types: ["locality", "political"]
    },
    {
            long_name: "Zhejiang",
            short_name: "Zhejiang",
            types: ["administrative_area_level_1",
                    "political"
            ]
    },
    {
            "long_name": "China",
            "short_name": "CN",
            types: ["country", "political"]
    }],
    formatted_address: "######, Hangzhou, Zhejiang, China"
 }

Is there any generic or common or standard rule which I can follow to map adddress_components keys for international countries for these values(address, address line 2, zipcode city and state) ?

0

There are 0 best solutions below