谷歌自动完成没有返回lat长

时间:2017-02-16 16:35:52

标签: 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">

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

2 个答案:

答案 0 :(得分:3)

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

getPlace.geometry.location.toUrlValue(6)将为您提供以逗号分隔的坐标。

代码段

&#13;
&#13;
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);
&#13;
<script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
<input id="searchTextField" type="text" size="50">
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您需要调用一个单独的Google API来获取地方信息。该文档位于this official link

请求网址如下。

https://maps.googleapis.com/maps/api/place/details/json?place_id=ChIJN1t_tDeuEmsRUsoyG83frY4&fields=geometry,name,rating,formatted_phone_number&key=YOUR_API_KEY