如何通过Java脚本输入邮政编码自动完成城市状态

How To Auto Complete City State By Entering Zipcode Through Java Script My Code Attached?

本文关键字:城市 状态 邮政编码 何通过 Java 脚本 输入      更新时间:2023-09-26

我通过使用谷歌地图API输入zipcode获得城市状态,我的代码如下,

google map API link,

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

通过邮政编码获取城市状态的脚本,

<script type="text/javascript">
   var zip ="34200";
    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);
                    document.getElementById("TextBox1").value=loc;
                }
            }
        });
        }
    }); 
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;
    }
</script>

这里我硬编码zip值"34200",现在不是从zip文本框中获取,而是在文本框"TextBox1"中显示城市状态,如下所示,

<asp:TextBox ID="txtZip"  runat="server" ToolTip="Enter Zip"></asp:TextBox>
   <asp:TextBox ID="TextBox1" runat="server" ForeColor="Gray"></asp:TextBox>

编辑:

如何使用这个jquery脚本与我的JS自动完成??

<script>
  $(function() {
    var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL",
      "ColdFusion",
      "Erlang",
      "Fortran",
      "Groovy",
      "Haskell",
      "Java",
      "JavaScript",
      "Lisp",
      "Perl",
      "PHP",
      "Python",
      "Ruby",
      "Scala",
      "Scheme"
    ];
    $( "#tags" ).autocomplete({
      source: availableTags
    });
  });
  </script>

我怎么能得到自动完成城市状态当我把邮政编码在"txtZip"??

希望你的建议

谢谢

从zip文本框中获取值,就像您设置城市一样。

var zip = document.getElementById("txtZip").value

希望能有所帮助