0

我通过使用谷歌地图 api 输入邮政编码来获取城市状态,我的代码如下,

谷歌地图api链接,

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

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

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

输入邮政编码值并在文本框“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>

希望得到您的建议

4

0 回答 0