Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6912851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:08:36+00:00 2026-05-27T09:08:36+00:00

Currenty, I’m using the following code to get the country, postal code, locality and

  • 0

Currenty, I’m using the following code to get the country, postal code, locality and sub-locality:

var country, postal_code, locality, sublocality;
for (i = 0; i < results[0].address_components.length; ++i)
{
    for (j = 0; j < results[0].address_components[i].types.length; ++j)
    {
        if (!country && results[0].address_components[i].types[j] == "country")
            country = results[0].address_components[i].long_name;
        else if (!postal_code && results[0].address_components[i].types[j] == "postal_code")
            postal_code = results[0].address_components[i].long_name;
        else if (!locality && results[0].address_components[i].types[j] == "locality")
            locality = results[0].address_components[i].long_name;
        else if (!sublocality && results[0].address_components[i].types[j] == "sublocality")
            sublocality = results[0].address_components[i].long_name;
    }
}

That’s unsatisfactory. Is there any other way to achieve the same result?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:08:37+00:00Added an answer on May 27, 2026 at 9:08 am
    if (typeof Object.keys == 'function')
        var length = function(x) { return Object.keys(x).length; };
    else
        var length = function() {};
    
    var location = {};      
    for (i = 0; i < results[0].address_components.length; ++i)
    {
        var component = results[0].address_components[i];
        if (!location.country && component.types.indexOf("country") > -1)
            location.country = component.long_name;
        else if (!location.postal_code && component.types.indexOf("postal_code") > -1)
            location.postal_code = component.long_name;
        else if (location.locality && component.types.indexOf("locality") > -1)
            location.locality = component.long_name;
        else if (location.sublocality && component.types.indexOf("sublocality") > -1)
            location.sublocality = component.long_name;
    
        // nothing will happen here if `Object.keys` isn't supported!
        if (length(location) == 4)
            break;
    }
    

    This is the most suitable solution for me. It may help someone too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am using following code to search my computer for files with certian extensions.
I have the following code in a UIView subclass that will move it off
I have some code that works and changes the style sheet using a form.
I am currenty using Apache's HttpComponents including the HttpAsyncClient beta, but I was wondering.
Currenty I'm working on a webshop using an auction module. This auction module has
Im currenty develoding a website using mvc 3 razor but have a little problem.
I have the following code, but it causes an exception. Without itoa games I
I need custom behavior for tab keys in RichTextEdit control. Currenty the best I
I am currenty doing a Sudoku game in c++. For that , I defined
I am currenty writing my master's thesis about monitoring of distributed systems. For this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.