使用邮政编码自动填写州城市

时间:2011-05-16 18:41:01

标签: javascript jquery google-maps jquery-plugins

嗨我有一个邮政编码字段,当用户输入一个5/9数字的拉链时,它应自动填充州和城市字段。

有没有这样的东西使用javascript或JQuery ???

由于

5 个答案:

答案 0 :(得分:18)

使用Javascript Google Maps V3 API:

您需要参考:

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

这样做:=

    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;
    }

这将以此格式返回包含城市和州的字符串,但您可以根据需要进行调整。

答案 1 :(得分:5)

答案 2 :(得分:2)

在Css-tricks.com上发现了一篇文章,功能是使用 Ziptastic 实现的 http://css-tricks.com/using-ziptastic/

$.ajax({
url: "http://zip.elevenbasetwo.com",
cache: false,
dataType: "json",
type: "GET",
data: "zip=" + el.val(),
success: function(result, success) {

$(".fancy-form div > div").slideDown(); /* Show the fields */

$("#city").val(result.city); /* Fill the data */
$("#state").val(result.state);

$(".zip-error").hide(); /* In case they failed once before */

$("#address-line-1").focus(); /* Put cursor where they need it */

},
error: function(result, success) {

$(".zip-error").show(); /* Ruh row */

}

});  

答案 3 :(得分:2)

RedLine有一个免费的邮政编码API。他们在这里有示例代码http://zipcodedistanceapi.redline13.com/Examples

答案 4 :(得分:0)

所有答案都表示赞赏,如果您不想进入更复杂的代码,这里有适合您的解决方案。

第一步

下载最新的国家/地区邮政编码Excel文件,并在根文件夹下找到该excel文件。(您可以在此处下载)
https://data.gov.in/catalog/all-india-pincode-pirectory#web_catalog_tabs_block_10

第二步:

调用ajax函数在哪里表单

<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type=”text/javascript”><br>
jQuery(document).ready(function(){
jQuery(‘.postcode‘).blur(function(){ //.postcode class of zipcode text field
var s = jQuery(this).val();
jQuery.ajax({
type: ‘POST’,
url:“http://www.sample.com/postcode.php“, //file which read zip code excel     file
dataType: “json”, //is used for return multiple values
data: { ‘s’ : s },
success: function(data){
try {
jQuery(“.state“).val(data.state); //region-class of state text field
jQuery(“.city“).val(data.dist);//city-class of city text filed
} catch (e) {
alert(e.Message);
}
},
error:function (xhr, status, err){
alert( “status=” + xhr.responseText + “, error=” + err );
}
});
});
});
</script>

此Ajax函数将调用“http://www.sample.com/postcode.php”文件

在postcode.php文件下,我们必须读取excel文件并返回州和城市值

<强> postcode.php

<?php
extract ($_POST);
$s=$_POST[‘s’];  //get value from ajax
$filename=”zip.csv“; //zipcode csv file(must reside in same folder)
$f = fopen($filename, “r”);
while ($row = fgetcsv($f))
{
if ($row[1] == $s) //1 mean number of column of zipcode
 {
  $district=$row[3];  //3- Number of city column
  $state=$row[4]; //4-Number of state column
  break;
 }
}
fclose($f);
echo json_encode(
 array(“dist” => $district,
 “state” => $state,
 “zip” => $s)
);  //Pass those details by json
?>

这就是全部,享受