Get (Retrieve) Country State City from Pin (Zip) code using JavaScript in ASP.Net

Last Reply 6 months ago By dharmendr

Posted 6 months ago

How can we get city and state automatically by pincode in

Posted 6 months ago

Hi itsme,

Use GoogleAPI to get the result.


<script type="text/javascript" src=""></script>
<script type="text/javascript">
    function GetDetails() {
        var geocoder = new google.maps.Geocoder();
        var postalCode = document.getElementById("txtPostalCode").value;
        geocoder.geocode({ 'address': postalCode }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var address = results[0].formatted_address;
                var pin = results[0].address_components[results[0].address_components.length - 1].long_name;
                var country = results[0].address_components[results[0].address_components.length - 2].long_name;
                var state = results[0].address_components[results[0].address_components.length - 3].long_name;
                var city = results[0].address_components[results[0].address_components.length - 4].long_name;
                document.getElementById('txtCountry').value = country;
                document.getElementById('txtState').value = state;
                document.getElementById('txtCity').value = city;
<input name="txtPostalCode" type="text" id="txtPostalCode" />
<input type="button" value="Get Details" onclick="GetDetails();" />
<br />
<table border="0" cellpadding="0" cellspacing="0">
        <td><input type="text" id="txtCountry" /></td>
        <td><input type="text" id="txtState" /></td>
        <td><input type="text" id="txtCity" /></td>