<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 是空的。我错过了什么吗?
最佳答案
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&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<input id="searchTextField" type="text" size="50">
关于google-maps - 谷歌自动完成不返回lat long,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42279720/