gpt4 book ai didi

javascript - 为什么我的 javascript 地理编码函数返回空白结果?

转载 作者:行者123 更新时间:2023-12-03 10:30:40 25 4
gpt4 key购买 nike

希望能帮到你。我创建了一个接收 lnglat 点对象并仅返回城镇的函数。我可以让它在 console.log 中打印正确的城镇,但它不会从函数返回数据。

我知道这将是一个基本错误,但有人可以看一下代码并告诉我吗?

提前致谢。

function getTownFromPoint(point){

var geocoder ;
var geocoder = new google.maps.Geocoder();
var townset = false;
mylocation = "";

geocoder.geocode({latLng: point}, function(results, status) {

if (status == google.maps.GeocoderStatus.OK) {

if (results[0]) {

var components=results[0].address_components;
for (var component=0;component<(components.length);component++){
if(components[component].types[0]=="country" & !townset){
mylocation = components[component].long_name;
}

if(components[component].types[0]=="postal_code" & !townset){
mylocation = components[component].long_name;
}

if(components[component].types[0]=="locality"){
mylocation = components[component].long_name;
townset = true;
console.log(mylocation);
}
}
}
}
});
return(mylocation);
}

最佳答案

地理编码器是异步的 - 您将在设置值之前返回值。

这里已经回答了:
Waiting for google maps geocoder?

关于javascript - 为什么我的 javascript 地理编码函数返回空白结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29232153/

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