【问题标题】:How do I get geolocation in openweather API如何在 openweathermAP 中获取地理位置
【发布时间】:2016-03-01 05:10:56
【问题描述】:

如何让 mu openweather API 用于地理定位?这是我当前的 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>

还有我的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');
    });

});

我得到了埃因霍温市的天气工作,但我希望能够将其调整为经纬度。 有人可以为我修复我的代码吗?并帮助我?

我知道这与此链接有关:api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon} 但我不知道如何实现我自己的源纬度和经度...

【问题讨论】:

    标签: javascript geolocation openweathermap


    【解决方案1】:

    您可以使用第三方 API 获取有关位置的数据, 例如:http://ip-api.com/

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

    接下来使用我们上面得到的 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)
        })
    })
    

    在这种情况下,摄氏温度(公制)

    或使用 HTML5 Geolocation API(在 Google Chrome 中只能使用 HTTPSlocalhost

    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)
            })
        })
    }
    

    【讨论】:

    • 谢谢!这似乎是一个很好的答案,但它仍然不起作用。首先获取弗里斯兰的当前位置,而我不在弗里斯兰。其次,没有天气。
    • @Maris ip-api 向您显示,您的位置在弗里斯兰,但您不在弗里斯兰?我理解正确吗?好的。第二。我在回答中写道$.getJSON(openWeatherMap, {.done(function(weather) { 必须在$.getJSON(getIP).done(function(location) {})
    • 是的,这是正确的。我做到了,但它不起作用。
    【解决方案2】:

    让我们试试吧,我希望这是一个更好的解决方案,通过使用 geolacation 和 Openweather API 来获取当前天气。只需传递您的 Openweather API 密钥,您将获得一个 json 数组,获取您的位置和当前天气。您还可以使用您想要的天气图标。

    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);
               })
            })  
        }
    }) 
    

    【讨论】:

      猜你喜欢
      • 2021-06-28
      • 1970-01-01
      • 2016-03-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多