【发布时间】:2021-05-11 14:48:50
【问题描述】:
我是网络开发的新手。今天,我正在尝试使用 Google Map API 使用 JavaScript 从经度和纬度获取国家名称和州名称。我从 Google Map API 阅读了文档并做了一些研究,但我对此有点困惑。我试了一下,这就是我所做的:
function getCountryName(latitude, longitude){
var country;
const geocoder = new google.maps.Geocoder();
geocoder.geocode({location: {lat: latitude, lng: longitude}}, (results, status) => {
if(status === "OK"){
if(results[0]){
country = results[0].address_components[0].types[0].country;
}
else{
country = "N/A";
}
}
});
return country;
}
但是,我不断收到“未定义”的结果。我的方法有什么问题吗? 提前谢谢大家!
【问题讨论】:
标签: javascript google-maps-api-3