我想从功能中获取数据并将其传递到提取网址,以获取城市数据

发布于 2025-02-02 23:42:59 字数 1205 浏览 4 评论 0原文

这是我下面正在处理的代码。我试图从第一个URL获取纬度和经度数据,以便我可以将其传递到一个新的提取请求中,该请求需要LAT和LON来抓住5天预测的城市中的天气数据。但是我不确定如何从一个函数中调用数据并将其作为字符串传递为URL中的字符串。

let weather = {
    "apiKey": "",
    "city": " ",

    fetchWeather: function(city) {
        // this fetch will call the api to grab the Lon and lat, using the function displayLatlon
        fetch("https://api.openweathermap.org/data/2.5/weather?q=" + city 
        + "&units=imperial&appid=" 
        + this.apiKey)

        .then(response => response.json())
        .then(data => this.displaylatlon(data))
        // This fetch will use the API to grab the weather conditions for the city based on it's lat and lon
        fetch("https://api.openweathermap.org/data/3.0/onecall?lat=" + **lat**
        + "&lon=" + **lon** 
        + "&exclude=hourly,daily&appid=" 
        + this.apiKey)

        .then(response => response.json())
        .then(weatherData =>console.log(weatherData))

    },
    // Function to grab the latitude and longitude of the city
     **displaylatlon: function(data) {
        const {lat, lon} = data.coord
        const{temp} = data.main**
        
        console.log(lat, lon)
     }
}

This is the code that I am working on below. I am trying to grab the latitude and longitude data from the 1st url, so that I can pass that into a new fetch request that takes lat and lon to grab weather data on a city with a 5 day forecast. But I am unsure how to call the data from one function and pass it as a string in the URL.

let weather = {
    "apiKey": "",
    "city": " ",

    fetchWeather: function(city) {
        // this fetch will call the api to grab the Lon and lat, using the function displayLatlon
        fetch("https://api.openweathermap.org/data/2.5/weather?q=" + city 
        + "&units=imperial&appid=" 
        + this.apiKey)

        .then(response => response.json())
        .then(data => this.displaylatlon(data))
        // This fetch will use the API to grab the weather conditions for the city based on it's lat and lon
        fetch("https://api.openweathermap.org/data/3.0/onecall?lat=" + **lat**
        + "&lon=" + **lon** 
        + "&exclude=hourly,daily&appid=" 
        + this.apiKey)

        .then(response => response.json())
        .then(weatherData =>console.log(weatherData))

    },
    // Function to grab the latitude and longitude of the city
     **displaylatlon: function(data) {
        const {lat, lon} = data.coord
        const{temp} = data.main**
        
        console.log(lat, lon)
     }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兰花执着 2025-02-09 23:42:59

粗略:

fetch("https://api.openweathermap.org/data/2.5/weather?q=" + city 
        + "&units=imperial&appid=" 
        + this.apiKey)
.then(response => response.json())
.then(data => {
   const {lat, lon} = data.coord;
   return fetch("https://api.openweathermap.org/data/3.0/onecall?lat=" + **lat**
        + "&lon=" + **lon** 
        + "&exclude=hourly,daily&appid=" 
        + this.apiKey)
})
.then(response => response.json())
.then(weatherData =>console.log(weatherData))

您可以打开{}使用多个代码的范围。

Roughly:

fetch("https://api.openweathermap.org/data/2.5/weather?q=" + city 
        + "&units=imperial&appid=" 
        + this.apiKey)
.then(response => response.json())
.then(data => {
   const {lat, lon} = data.coord;
   return fetch("https://api.openweathermap.org/data/3.0/onecall?lat=" + **lat**
        + "&lon=" + **lon** 
        + "&exclude=hourly,daily&appid=" 
        + this.apiKey)
})
.then(response => response.json())
.then(weatherData =>console.log(weatherData))

You can open a {} scope to use multiple lines of code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文