【问题标题】:Using Zipcode to get the city,state and country Google Map API使用 Zipcode 获取城市、州和国家 Google Map API
【发布时间】:2022-09-26 13:44:30
【问题描述】:

我使用 Google Map API 获取城市名称、州和国家/地区,并使用我网站上的邮政邮政编码。谷歌地址自动完成工作正常。但我不需要自动完成。只需我提供邮政编码并单击按钮,它就会提供地址。

我面临的问题是 TypeError: Cannot read properties of undefined (reading \'activeElement\')

我的代码很简单

<input type = \"text\" id=\"deliverTo\" name=\"deliverTo\" value = \"\"/>
<input type =\"button\" value =\"Get Address\" name =\"Submit\" onclick = \"getcoastalzip()\" />

<input type = \"text\" id = \"ship_city\"  name =\"ship_city\" Placeholder=\"City\" />
<input type = \"text\" id = \"ship_state\"  name =\"ship_state\" Placeholder=\"State\" />
<input type = \"text\" id = \"ship_country\"  name =\"ship_country\" Placeholder=\"Country\" />
<input type = \"text\" id = \"ship_postal_code\"  name =\"ship_postal_code\" Placeholder=\"Postal Code\" />
<script src=\"https://maps.google.com/maps/api/js?key=APIKEY=places\" type=\"text/javascript\"></script>

<script>
function getcoastalzip() {
    var input = document.getElementById(\'deliverTo\').value;
    
    alert(input);
    

    var autocomplete = new google.maps.places.Autocomplete(input);
    console.log(autocomplete);
        autocomplete.addListener(\'place_changed\', function() {
     

     
       var place = autocomplete.getPlace();
   
   console.log(place);
    
    console.log(place[\'address_components\'][0][\'long_name\']);
    console.log(place[\'address_components\'][1][\'long_name\']);
    document.getElementById(\'ship_city\').value = place[\'address_components\'][1][\'long_name\'];
    document.getElementById(\'ship_state\').value = place[\'address_components\'][2][\'long_name\'];
    document.getElementById(\'ship_postal_code\').value = place[\'address_components\'][0][\'long_name\'];
     document.getElementById(\'ship_country\').value = place[\'address_components\'][3][\'long_name\'];

   
});
 }
</script>

  • 如果您不需要autocomplete,为什么要使用它而不是Geocoding API
  • 我的概念是使用邮政编码显示城市、州和国家(在输入框中输入邮政编码并单击按钮,它提供城市、州和国家)
  • 邮政编码不是唯一的,您可以在不同的国家/地区找到相同的邮政编码。

标签: javascript google-maps google-maps-api-3


【解决方案1】:

不,您不能使用代码来获取所需的详细信息,因为它们不是唯一的。每个国家/地区都有自己的邮政编码系统,有时它们确实重叠。

【讨论】:

    猜你喜欢
    • 2017-09-02
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 2010-11-17
    • 2012-11-18
    • 1970-01-01
    • 2019-05-27
    • 2017-11-11
    相关资源
    最近更新 更多