如何通过自动完成获取街道地址 google api

How to get street level address by autocomplete google api

我想获取街道地址,但无法获取任何信息。

提前致谢。

  <script>   
    google.maps.event.addDomListener(window, 'load', function () {
        var options = {
            types: ['(address)'],
            componentRestrictions: ['pk']
        };

        var input1 = document.getElementById('txtUserStartLocation');
        var places = new google.maps.places.Autocomplete(input1, options);

        var input2 = document.getElementById('txtUserDistination');
        places = new google.maps.places.Autocomplete(input2, options)

    });
</script>

错误是你使用的是(address)类型值,根据文档必须是address

https://developers.google.com/places/supported_types#table3

看看这个例子:

var autocomplete;

function initAutocomplete() {
  autocomplete = new google.maps.places.Autocomplete(
      document.getElementById('autocomplete'),
      {
          types: ['address'],
          componentRestrictions: {
              country: ['pk']
          }
      });
}
#map {
  height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}
#autocomplete {
   width: 400px;
}
<div id="locationField">
    <input id="autocomplete" placeholder="Enter your address" type="text"></input>
</div>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDztlrk_3CnzGHo7CFvLFqE_2bUKEq1JEU&libraries=places&callback=initAutocomplete" async defer></script>

希望对您有所帮助!