如何通过自动完成谷歌api获得街道级地址

时间:2018-02-17 10:14:48

标签: google-maps google-places-api googleplacesautocomplete

我想获得街道地址,但无法得到任何东西。

提前感谢。

  <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>

1 个答案:

答案 0 :(得分:1)

错误在于您使用的是(address)类型值,根据文档,它必须是address

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

看一下这个例子:

&#13;
&#13;
var autocomplete;

function initAutocomplete() {
  autocomplete = new google.maps.places.Autocomplete(
      document.getElementById('autocomplete'),
      {
          types: ['address'],
          componentRestrictions: {
              country: ['pk']
          }
      });
}
&#13;
#map {
  height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}
#autocomplete {
   width: 400px;
}
&#13;
<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>
&#13;
&#13;
&#13;

我希望这有帮助!