谷歌自动完成没有返回纬度很长时间

van*_*lay 2 google-maps

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

<script>
function initialize() {

    var input = document.getElementById('searchTextField');
    var autocomplete = new google.maps.places.Autocomplete(input);

    autocomplete.addListener('place_changed', function(){
        console.log(autocomplete.getPlace());
    });
}

    google.maps.event.addDomListener(window, 'load', initialize);
</script>

<input id="searchTextField" type="text" size="50">
Run Code Online (Sandbox Code Playgroud)

从浏览器的控制台中,我可以看到 getPlace.geometry.location.lat 是空的。我错过了什么吗?

geo*_*zip 5

getPlace.geometry.location.lat是一个函数,你需要调用它来获取它的值 ( getPlace.geometry.location.lat())。

getPlace.geometry.location.toUrlValue(6) 会给你逗号分隔的坐标。

代码片段:

function initialize() {
  var input = document.getElementById('searchTextField');
  var autocomplete = new google.maps.places.Autocomplete(input);

  autocomplete.addListener('place_changed', function() {
    console.log("lat=" + autocomplete.getPlace().geometry.location.lat());
    console.log(autocomplete.getPlace().geometry.location.toUrlValue(6));
  });
}

google.maps.event.addDomListener(window, 'load', initialize);
Run Code Online (Sandbox Code Playgroud)
<script src="https://maps.googleapis.com/maps/api/js?libraries=places&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<input id="searchTextField" type="text" size="50">
Run Code Online (Sandbox Code Playgroud)