无法加载地方自动完成地址

时间:2019-05-27 02:18:47

标签: google-maps-api-3

我无法使用Google Maps Places API加载地址。键入地址后,不会加载任何内容。我正在使用以下代码加载地址。

HTML

<div class="main-search-input-item location">
                        <div id="autocomplete-container">
                            <input type="text" placeholder="Location" id="locality" name="locality">
                        </div>
                        <a href="#"><i class="fa fa-map-marker"></i></a>
                    </div>

javascript

<script>
  function initAutocomplete() {
var input = document.getElementById('autocomplete-input');
var autocomplete = new google.maps.places.Autocomplete(input);

autocomplete.addListener('place_changed', function() {
  var place = autocomplete.getPlace();
  if (!place.geometry) {
    window.alert("No details available for input: '" + place.name + "'");
    return;
  }
});

if ($('.main-search-input-item')[0]) {
    setTimeout(function(){ 
        $(".pac-container").prependTo("#autocomplete-container");
    }, 300);
}
}
</script>
<script src="https://maps.googleapis.com/maps/api/js? 
  key=API_KEY&libraries=places&callback=initAutocomplete"></script>

0 个答案:

没有答案