【问题标题】:Getting Postcode From Google Map Search从 Google 地图搜索中获取邮政编码
【发布时间】:2019-08-10 19:54:28
【问题描述】:

我的代码可以从 long/lat 中找到邮政编码,我可以将其与下面的代码一起使用,但是(我认为)这将被视为几个请求(不想消耗我的 google 配额)。

有没有其他方法可以让我在下面显示邮政编码作为自动完成的一部分,最好是在另一个字段中..

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />




  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>


 <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=HERE&amp;libraries=places"></script>

<script>
  google.maps.event.addDomListener(window, 'load', initialize);
    function initialize() {
      var input = document.getElementById('autocomplete_search');
      var autocomplete = new google.maps.places.Autocomplete(input);
      autocomplete.addListener('place_changed', function () {
      var place = autocomplete.getPlace();

      // place variable will have all the information you are looking for.
      $('#lat').val(place.geometry['location'].lat());
      $('#long').val(place.geometry['location'].lng());
    });
  }
</script>


  <title>Google Places Autocomplete InputBox Example Without Showing Map - Tutsmake.com</title>
 <style>
    .container{
    padding: 10%;
    text-align: center;
   } 
 </style>
</head>
<body>

<div class="container">
    <div class="row">
        <div class="col-12"><h2>Google Places Autocomplete InputBox Example Without Showing Map</h2></div>
        <div class="col-12">
            <div id="custom-search-input">
                <div class="input-group">
                    <input id="autocomplete_search" name="autocomplete_search" type="text" class="form-control" placeholder="Search" />
                    <input type="hidden" name="lat">
                    <input type="hidden" name="long">
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>

【问题讨论】:

    标签: google-maps


    【解决方案1】:

    您可以获取 PlaceResult 的地址组件并解析它们以获得邮政编码,就像在 Place Autocomplete Address Form Example 中所做的那样

    autocomplete.addListener('place_changed', function () {
      var place = autocomplete.getPlace();
      for (var component in componentForm) {
        document.getElementById(component).value = '';
        document.getElementById(component).disabled = false;
      }
    
      // Get each component of the address from the place details,
      // and then fill-in the corresponding field on the form.
      for (var i = 0; i < place.address_components.length; i++) {
        var addressType = place.address_components[i].types[0];
        if (componentForm[addressType]) {
          var val = place.address_components[i][componentForm[addressType]];
          document.getElementById(addressType).value = val;
        }
      }
    
      // place variable will have all the information you are looking for.
      $('#lat').val(place.geometry.location.lat());
      $('#long').val(place.geometry.location.lng());
    });
    

    proof of concept fiddle

    【讨论】:

    • 谢谢。我在寻找答案时遇到了它,但是当我在顶部字段中放置地址时,似乎无法填充邮政编码。
    • 您在我的回答中看到fiddle 的问题吗?如果是这样,您对哪个地址有疑问(并非所有地方都有邮政编码)。如果不是,那么您的密钥可能存在问题(以及不同/新问题)。您收到了哪些错误消息?
    • 美国地址似乎没问题,但使用已知的英国地址时,它不显示邮政编码。
    猜你喜欢
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多