我正在使用谷歌地图 API 从坐标中获取城市名称。当我获取 API url 时,我得到一个空的 json
unction callWCoords(position) {
let latitude = position.coords.latitude;
let longitude =position.coords.longitude;
let call1;
let API1="https://maps.googleapis.com/maps/api/geocode/json?latlng=+"+latitude+","+longitude+"&key=AIzaSyAfH3Ypu0Al8HpNRXhOPEzGLeNbkxOlsoI"
fetch(API1).then(
response=>{alert(JSON.stringify(response));
}).then( data=>{
})
//getPlaceC(call1);
}
JSON.stringfy(response)
返回"{}"
如果我尝试 JSON.stringfy(data)
也是如此在第二个 .then block 中。因为在官方的 google json 中我应该得到一个名为 results
的数组我尝试替换 results
与 data
但没有成功。
最佳答案
function callWCoords(position) {
let latitude = position.coords.latitude;
let longitude =position.coords.longitude;
let API1="https://maps.googleapis.com/maps/api/geocode/json?latlng=+"+latitude+","+longitude+"&key=AIzaSyAfH3Ypu0Al8HpNRXhOPEzGLeNbkxOlsoI"
fetch(API1).then(res => res.json()).then(data => console.log(data))
}
您必须在获取返回时调用 res.json() 函数才能获取数据。
此外,您不会在警报后返回数据,这基本上是从下一个 .then
关于javascript - 从谷歌地图 api url 获取空 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58256379/