简体   繁体   English

如何在 openweather API 中获取地理位置

[英]How do I get geolocation in openweather API

How do I get mu openweather API to work with geolocation?如何让 mu openweather API 与地理定位一起使用? This is my current html code:这是我当前的 html 代码:

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="http://s3.amazonaws.com/codecademy-content/courses/ltp/css/bootstrap.css">
        <link rel="stylesheet" href="main.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script type='text/javascript' src='app.js'></script>
    </head>
    <body>
        <div class="jumbotron">
            <button onclick="getLocation()">Get my location.</button>
            <p id="demo"></p>

            <script>
                var x = document.getElementById("demo");
                function getLocation() {
                    if (navigator.geolocation) {
                        navigator.geolocation.getCurrentPosition(showPosition);
                    } else { 
                        x.innerHTML = "Geolocation is not supported by this browser.";
                    }
                }

                function showPosition(position) {
                    x.innerHTML = "Latitude: " + position.coords.latitude + 
                                  "<br>Longitude: " + position.coords.longitude;    
                }
            </script>

            <p>The weather outside is: </p> 
            <div class= "weather">
                Oops.. there is no temperature available for your location right now.
            </div>
        </div>                                  
    </body>
</html>

And my JavaScript code:还有我的JavaScript代码:

$(document).ready(function(){
    $.getJSON( "http://api.openweathermap.org/data/2.5/weather?q=Eindhoven&appid=9334f947893792dcb9b2e2c05ae23eb0", function( data ) {
        $('.weather').html(Math.round(data.main.temp-273)+ ' degrees Celcius');
    });

});

I got the weather in Eindhoven city working, but I want to be able to adjust it to the latitude and longitude.我让埃因霍温市的天气正常工作,但我希望能够将其调整为经纬度。 Can someone fix my code for me?有人可以帮我修复我的代码吗? And help me?并帮助我?

I know it has something to do with this link: api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon} but I don't know how to implement my own fount latitude and longitude in it...我知道它与此链接有关:api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon} 但我不知道如何在其中实现我自己的 fount 纬度和经度...

You can get data about the location with use third-party API, for example: http://ip-api.com/您可以使用第三方 API 获取有关位置的数据,例如: http : //ip-api.com/

var getIP = 'http://ip-api.com/json/';
$.getJSON(getIP).done(function(location) {
    console.log(location)
})

Next get WeatherData from OpenWeatherMap service using the ip-api that we got above接下来使用我们上面得到的 ip-api 从 OpenWeatherMap 服务获取 WeatherData

var getIP = 'http://ip-api.com/json/';
var openWeatherMap = 'http://api.openweathermap.org/data/2.5/weather'
$.getJSON(getIP).done(function(location) {
    $.getJSON(openWeatherMap, {
        lat: location.lat,
        lon: location.lon,
        units: 'metric',
        APPID: 'APIKEY'
    }).done(function(weather) {
        console.log(weather)
    })
})

In this case, the celsius temperature (metric)在这种情况下,摄氏度(公制)

Or using HTML5 Geolocation API (in Google Chrome work only with HTTPS or on localhost )或使用 HTML5 Geolocation API(在 Google Chrome 中仅使用HTTPS或在localhost

var openWeatherMap = 'http://api.openweathermap.org/data/2.5/weather'
if (window.navigator && window.navigator.geolocation) {
    window.navigator.geolocation.getCurrentPosition(function(position) {
        $.getJSON(openWeatherMap, {
            lat: position.coords.latitude,
            lon: position.coords.longitude,
            units: 'metric',
            APPID: 'APIKEY'
        }).done(function(weather) {
            console.log(weather)
        })
    })
}

Lets try it , I hope that is a better solution to get current weather via using geolacation and Openweather API .让我们尝试一下,我希望这是通过使用 geolacation 和 Openweather API 获取当前天气的更好解决方案。 Simply pass your Openweather API Key , you will get a json array, fetch your location and current weather.只需传递您的 Openweather API Key ,您将获得一个 json 数组,获取您的位置和当前天气。 And you also use your weather icon which you want.您还可以使用您想要的天气图标。

window.addEventListener("load",() =>{
    let long;
    let lag;
    
    if(navigator.geolocation){
        navigator.geolocation.getCurrentPosition((position) =>{
        var lat = position.coords.latitude;
        var long = position.coords.longitude;
        const proxy = "https://cors-anywhere.herokuapp.com/";
        const api = `${proxy}api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${long}&appid=9891731b361e47661f72b06213efbf65`;
        fetch(api) 
           .then((response) =>{
               return response.json();
           })
           .then(data =>{
               const {name} = data;
               const {feels_like} = data.main;
               const {id,main} = data.weather[0];
               loc.textContent = name;
               climate.textContent = main;
               tempValue.textContent = Math.round(feels_like-273);
               if(id < 250){
                   tempIcon.src = './icon/storm.svg'
               } 
               else if(id < 350){
                tempIcon.src = './icon/drizzle.svg'  
               }
               else if(id < 550){
                tempIcon.src = './icon/rain.svg'  
               }
               else if(id < 650){
                tempIcon.src = './icon/snow.svg'  
               }
               else if(id < 800){
                tempIcon.src = './icon/atmosphere.svg'  
               }
               else if(id === 800){
                tempIcon.src = './icon/clear.svg'  
               }
               else if(id >800){
                tempIcon.src = './icon/clouds.svg'  
               }
               console.log(data);
           })
        })  
    }
}) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OpenWeather API,如何将响应从一个响应传递到另一个调用? - OpenWeather API, How do I pass in response from one response to another call? GET请求openweather API不起作用 - GET request for openweather API not working 如何使用 HTML5 地理定位或 Google API 获取地址经纬度? - How do I get an addresses latitude-longitude using HTML5 geolocation or Google API? 如何从我的嵌套 function 中获取地理定位 API 坐标并进入 React state? - How do I get Geolocation API co-ordinates out of my nested function and into React state? 如何使用Cordova Geolocation API进行反向地理编码以获得城市和国家? - How to do reverse geocoding with Cordova Geolocation API to get City and Country? 如何将Geolocation整合到Google Places API中? - How do I incorporate Geolocation into the Google Places API? Cordova-我怎么知道我的应用程序是使用HTML5 Geolocation API还是Cordova Geolocation插件? - Cordova - how do I know if my app is using the HTML5 Geolocation API or the cordova geolocation plugin? 如何获取和设置Google地图的地理位置? - How do I Get and Set geolocation to google maps? 我正在尝试从Openweather API获得响应,但是却收到404 - I am trying to get response from the Openweather API but I am getting a 404 如何在 OpenWeather 的 api url 中使用模板字符串 - How to use template strings in an api url for OpenWeather
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM