Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

auto fill for state city using zipcode

Hi i have a zipcode field when user enters a 5/9 digit zip it should auto populate the State and city fields.

Is there any thing like this usin javascript or JQuery???

Thanks

like image 813
user695663 Avatar asked May 16 '11 18:05

user695663


People also ask

How do I autofill a ZIP code in Excel?

Select the cell or range of cells that you want to format. , click the arrow, and then click More Number Formats. In the Format Cells dialog box, under Category, click Special. In the Type list, click Zip Code or Zip Code + 4.

What does ZIP mean when filling out a form?

ZIP is an acronym for Zone Improvement Plan. However, the USPS intentionally chose the acronym to indicate that mail travels more quickly when senders mark the postal code on their packages and envelopes.

Can ZIP codes span multiple states?

ZIP codes can and do cross state lines (rarely, but just enough to cause some problems and confusion), county lines (about 10% of ZIPs are in more than one county), political jurisdictions (cities, congressional districts), metro areas, etc.


1 Answers

Using the Javascript Google Maps V3 API:

You need to reference this:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>

Do this:=

    var zip = <yourzipcode>;
    var lat;
    var lng;
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({ 'address': zip }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            geocoder.geocode({'latLng': results[0].geometry.location}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                if (results[1]) {
                    var loc = getCityState(results);
                }
            }
        });
        }
    }); 

function getCityState(results)
    {
        var a = results[0].address_components;
        var city, state;
        for(i = 0; i <  a.length; ++i)
        {
           var t = a[i].types;
           if(compIsType(t, 'administrative_area_level_1'))
              state = a[i].long_name; //store the state
           else if(compIsType(t, 'locality'))
              city = a[i].long_name; //store the city
        }
        return (city + ', ' + state)
    }

function compIsType(t, s) { 
       for(z = 0; z < t.length; ++z) 
          if(t[z] == s)
             return true;
       return false;
    }

This all returns a string containing the city and state in this format , but you can adjust this to your needs.

like image 91
slandau Avatar answered Oct 08 '22 13:10

slandau