Google AJAX API and pulling in IP county and state

by johlum
5 replies
Was wondering anyone had success pulling in the state of the person hitting a web page using (google.loader.ClientLocation.address.region) and getting the state name not abbreviated?

I get HI, but want Hawaii.

Can it be done?

Thanks

Ernie
#ajax #api #county #google #pulling #state
  • Profile picture of the author lisag
    Originally Posted by johlum View Post

    Was wondering anyone had success pulling in the state of the person hitting a web page using (google.loader.ClientLocation.address.region) and getting the state name not abbreviated?

    I get HI, but want Hawaii.

    Can it be done?

    Thanks

    Ernie
    Region is universally coded as the two character abbreviation, even in non-Google databases like this one: http://www.maxmind.com/download/geoi...iteCity.dat.gz.

    The best you can do is take the region value and compare it against an array of state names, where the 2 character region is the key, and use the returned value.

    Code:
           = array
          (
          'AL'=>"Alabama",  
          'AK'=>"Alaska",  
          'AZ'=>"Arizona",  
          'AR'=>"Arkansas",  
          'CA'=>"California",  
          'CO'=>"Colorado",  
          'CT'=>"Connecticut",  
          'DE'=>"Delaware",  
          'DC'=>"District Of Columbia",  
          'FL'=>"Florida",  
          'GA'=>"Georgia",  
          'HI'=>"Hawaii",  
          'ID'=>"Idaho",  
          'IL'=>"Illinois",  
          'IN'=>"Indiana",  
          'IA'=>"Iowa",  
          'KS'=>"Kansas",  
          'KY'=>"Kentucky",  
          'LA'=>"Louisiana",  
          'ME'=>"Maine",  
          'MD'=>"Maryland",  
          'MA'=>"Massachusetts",  
          'MI'=>"Michigan",  
          'MN'=>"Minnesota",  
          'MS'=>"Mississippi",  
          'MO'=>"Missouri",  
          'MT'=>"Montana",
          'NE'=>"Nebraska",
          'NV'=>"Nevada",
          'NH'=>"New Hampshire",
          'NJ'=>"New Jersey",
          'NM'=>"New Mexico",
          'NY'=>"New York",
          'NC'=>"North Carolina",
          'ND'=>"North Dakota",
          'OH'=>"Ohio",  
          'OK'=>"Oklahoma",  
          'OR'=>"Oregon",  
          'PA'=>"Pennsylvania",  
          'RI'=>"Rhode Island",  
          'SC'=>"South Carolina",  
          'SD'=>"South Dakota",
          'TN'=>"Tennessee",  
          'TX'=>"Texas",  
          'UT'=>"Utah",  
          'VT'=>"Vermont",  
          'VA'=>"Virginia",  
          'WA'=>"Washington",  
          'WV'=>"West Virginia",  
          'WI'=>"Wisconsin",  
          'WY'=>"Wyoming"
       );
    Code:
     
    Signature

    -- Lisa G

    {{ DiscussionBoard.errors[1830655].message }}
    • Profile picture of the author johlum
      Thanks!

      Now I just need to figure out how to insert this array in my javasacript. :-)

      Ernie
      {{ DiscussionBoard.errors[1831634].message }}
      • Profile picture of the author lisag
        Originally Posted by johlum View Post

        Thanks!

        Now I just need to figure out how to insert this array in my javasacript. :-)

        Ernie
        That's a PHP array. JavaScript isn't going to like it much. I was illustrating the solution and I ASSumed PHP.

        I know how to do that array in JS.
        Signature

        -- Lisa G

        {{ DiscussionBoard.errors[1831960].message }}
      • Profile picture of the author CMartin
        Originally Posted by johlum View Post

        Now I just need to figure out how to insert this array in my javasacript. :-)
        Ernie
        Try this
        HTML Code:
        <script type="text/javascript">
        
        var states = {};
        states["AL"] = "Alabama";
        states["AK"] = "Alaska";
        states["AZ"] = "Arizona";
        states["AR"] = "Arkansas";
        states["CA"] = "California";
        states["CO"] = "Colorado";
        states["CT"] = "Connecticut";
        states["DE"] = "Delaware";
        states["DC"] = "District Of Columbia";
        states["FL"] = "Florida";
        states["GA"] = "Georgia";
        states["HI"] = "Hawaii";
        states["ID"] = "Idaho";
        states["IL"] = "Illinois";
        states["IN"] = "Indiana";
        states["IA"] = "Iowa";
        states["KS"] = "Kansas";
        states["KY"] = "Kentucky";
        states["LA"] = "Louisiana";
        states["ME"] = "Maine";
        states["MD"] = "Maryland";
        states["MA"] = "Massachusetts";
        states["MI"] = "Michigan";
        states["MN"] = "Minnesota";
        states["MS"] = "Mississippi";
        states["MO"] = "Missouri";
        states["MT"] = "Montana",
        states["NE"] = "Nebraska",
        states["NV"] = "Nevada",
        states["NH"] = "New Hampshire",
        states["NJ"] = "New Jersey",
        states["NM"] = "New Mexico",
        states["NY"] = "New York",
        states["NC"] = "North Carolina",
        states["ND"] = "North Dakota",
        states["OH"] = "Ohio";
        states["OK"] = "Oklahoma";
        states["OR"] = "Oregon";
        states["PA"] = "Pennsylvania";
        states["RI"] = "Rhode Island";
        states["SC"] = "South Carolina";
        states["SD"] = "South Dakota",
        states["TN"] = "Tennessee";
        states["TX"] = "Texas";
        states["UT"] = "Utah";
        states["VT"] = "Vermont";
        states["VA"] = "Virginia";
        states["WA"] = "Washington";
        states["WV"] = "West Virginia";
        states["WI"] = "Wisconsin";
        states["WY"] = "Wyoming";
        
        
        function getState( theState ) {
            s = "";
            if( theState in states ) {
                s = states[theState];
            }
            return s;
        }
        
        myState = "TX";
        
        document.write( getState(myState) );
        
        </script>
        HTH

        Carlos
        {{ DiscussionBoard.errors[1832233].message }}
        • Profile picture of the author johlum
          Thanks.

          Lisa solved this for me.

          Highly recommend her services.

          Ernie
          {{ DiscussionBoard.errors[1834899].message }}

Trending Topics