本文介绍了从Google Map Search获取邮政编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有可以从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(窗口,加载",初始化);函数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变量将包含您要查找的所有信息.$('#lat').val(place.geometry ['location'].lat());$('#long').val(place.geometry ['location'].lng());});}</script>< title>未显示地图的Google地方信息自动填充输入框示例-Tutsmake.com</title>< style>.容器{填充:10%;文本对齐:居中;}</style></head><身体>< div class ="container">< div class ="row">< div class ="col-12">< h2>不显示地图的Google地方自动填充输入框示例</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"/><输入type ="hidden" name ="lat"><输入type ="hidden" name ="long"></div></div></div></div></div></body></html> 
解决方案

您可以获取PlaceResult的地址组成部分,并按照

I have code that will find postcode from long/lat, which I could use with the code below, but (I think) this will be seen as several requests (dont want to burn my google quota).

Is there any other way that I can get the below could to show postcode as part of the auto complete, ideally in another field..

<!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>
解决方案

You can get the address components of the PlaceResult and parse them for the postal code as is done in the 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

这篇关于从Google Map Search获取邮政编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 05:27