寻找城市,州,和用户输入邮政编码县(仅限美国邮政编码)

时间:2017-11-05 22:59:29

标签: javascript google-maps

我需要使用JavaScript找到用户输入的邮政编码或用户输入的城市/州的城市,州和县。

据我所知,美国的一些(极少数)城市/邮政编码位于2个或更多县。

我正在使用Google Maps Geocoding API来完成此操作,但我遇到某些邮政编码问题。

适用于以下(以及其他许多人):

  • 90210(城市:比佛利山州:CA县:洛杉矶县)
  • 55101(城市:圣保罗州:MN县:拉姆齐县)
  • 77001(城市:休斯顿州:TX县:哈里斯县)

对以下(以及其他许多人)的工作不正确:

  • 24501(林奇堡,弗吉尼亚州) - 不显示任何内容
  • 10001(纽约州纽约市) - 不显示任何内容

我向Google Maps Geocoding API提出了2个请求。第一个请求是查找用户输入的邮政编码的城市和州。第二个请求采取城市和州,并找到县。我打算为每个请求使用我的Geocoding API密钥,但它在没有本地测试的情况下工作正常。

我的代码如下:

function submit_form() {

    var city;
    var state;
    var county;

    // Get the user's input
    var zip = document.getElementById("user_input").value;

    // Make a request to the Google Maps Geocoding API
    // Find city and state of the user's zip code
    $.getJSON('https://maps.googleapis.com/maps/api/geocode/json?address='+zip, function(data){

              var address_components = data.results[0].address_components;

              $.each(address_components, function(index, component){
                     var types = component.types;

                     $.each(types, function(index, type){
                            if(type == 'locality') {
                                city = component.long_name;
                            }
                            if(type == 'administrative_area_level_1') {
                                state = component.short_name;
                            }
                     });
              });

              callCounty();
    });

    // Make a request to the Google Maps Geocoding API
    // Find the county of the city and state
    function callCounty() {
        $.getJSON('https://maps.googleapis.com/maps/api/geocode/json?address='+city+state, function(data){

            var address_components = data.results[0].address_components;

              $.each(address_components, function(index, component){
                     var types = component.types;

                     $.each(types, function(index, type){
                            if(type == 'administrative_area_level_2') {
                                county = component.short_name;
                                waitToCall();
                            }
                     });
              });
        });
    }

    function waitToCall() {
        document.getElementById('log').innerHTML += "<p><strong>City:</strong> " + city + " <strong>State:</strong> " + state + " <strong>County:</strong> " + county + "</p>";
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<h1>Find Your City, State, and County</h1>
<p>Enter your zip code or your city, state:</p>
<form>
<input type="text" name="user_input" id="user_input" placeholder="City, State, or Zip code" />
<input type="button" value="Submit" onclick="submit_form()"/>
</form>
<div id="log"><p>Results will show here.</p></div>

1 个答案:

答案 0 :(得分:0)

此示例尝试从邮政编码请求中取出该县。如果未找到,则会尝试城市/州请求。我将内部的每个循环更改为indexOf查找。

&#13;
&#13;
function submit_form() {

    var city;
    var state;
    var county;

    // Get the user's input
    var zip = document.getElementById("user_input").value;

    // Make a request to the Google Maps Geocoding API
    // Find city and state of the user's zip code
    $.getJSON('https://maps.googleapis.com/maps/api/geocode/json?address='+zip, function(data){

      var address_components = data.results[0].address_components;

      $.each(address_components, function(index, component){
        var types = component.types;
        if (types.indexOf('locality') > -1)
          city = component.long_name;
        else if (types.indexOf('administrative_area_level_1') > -1)
          state = component.short_name;
        else if (types.indexOf('administrative_area_level_2') > -1)
          county = component.short_name;
      });
              
      if (!county)
        callCounty();
      else
        waitToCall();
    });

    // Make a request to the Google Maps Geocoding API
    // Find the county of the city and state
    function callCounty() {
        $.getJSON('https://maps.googleapis.com/maps/api/geocode/json?address='+city+state,          
          function(data){
            var address_components = data.results[0].address_components;
            $.each(address_components, function(index, component){
              var types = component.types;
              if (types.indexOf('administrative_area_level_2') > -1) 
                county = component.short_name;
            });
            if (county)
              waitToCall();
              
          });
    }

    function waitToCall() {
        document.getElementById('log').innerHTML += "<p><strong>City:</strong> " + city + " <strong>State:</strong> " + state + " <strong>County:</strong> " + county + "</p>";
    }
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<h1>Find Your City, State, and County</h1>
<p>Enter your zip code or your city, state:</p>
<form>
<input type="text" name="user_input" id="user_input" placeholder="City, State, or Zip code" />
<input type="button" value="Submit" onclick="submit_form()"/>
</form>
<div id="log"><p>Results will show here.</p></div>
&#13;
&#13;
&#13;