2017-02-16 53 views
0
<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是空的。我错过了什么吗?谷歌自动完成不返回拉特长

+0

'getPlace.geometry.location.lat'是一个函数,你需要调用它来获得它的值。 – geocodezip

+0

@geocodezip是的,我知道。但是我可以从浏览器的控制台看到它的值是空的? – vandelay

+0

你期望看到函数的值是什么? – geocodezip

回答

2

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

+0

好吧,非常感谢。我认为它是空的,因为我无法在浏览器控制台中找到geometry - > location - > lat的值,我可以展开字典。 – vandelay