所以我有一个提取请求,当你这样做时,它会给我一个城市的Lat和Lon;convertToCords。GetCords("伦敦"(;在控制台中。但是,我该如何将Lat和Lon保存为单独的变量,以便在另一次提取中使用??
这是我的JS
var convertToCords = {
Key: "c9676e6950f05e39b2aae36c413d9dff",
GetCords: function (cityname) {
fetch(
"https://api.openweathermap.org/geo/1.0/direct?q=" + cityname + "&limit=1&appid=c9676e6950f05e39b2aae36c413d9dff"
)
.then((response) => response.json())
.then((data) => console.log(data));
},
showresults: function(data){
}
}
以下是控制台给我的
{name: 'London', local_names: {…}, lat: 51.5073219, lon: -0.1276474,
这对你有用吗?
var convertToCords = {
key: "---",
GetCords: async function(cityname) {
const response = await fetch(
"https://api.openweathermap.org/geo/1.0/direct?q=" + cityname + "&limit=1&appid=c9676e6950f05e39b2aae36c413d9dff"
);
return await response.json();
},
ShowResults: async function() {
let [{lat, lon}] = await convertToCords.GetCords("paris")
// do something with lat and lon
console.log(lat, lon)
}
}
convertToCords.ShowResults()
顺便说一句,永远不要公开显示你的api密钥