gpt4 book ai didi

google-maps - 谷歌自动完成不返回lat long

转载 作者:行者123 更新时间:2023-12-04 13:31:14 26 4
gpt4 key购买 nike

<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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com