【问题标题】:Cannot select JSON elements无法选择 JSON 元素
【发布时间】:2017-06-26 21:19:35
【问题描述】:

我是 Web 开发新手,但我正在尝试使用 openweatherapp API:https://openweathermap.org/current#geo 创建一个天气应用程序。 JSON对象如下:

{"coord":{"lon":5.38,"lat":34.72},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"base":"stations","main":{"temp":280.903,"pressure":1000.23,"humidity":65,"temp_min":280.903,"temp_max":280.903,"sea_level":1031.37,"grnd_level":1000.23},"wind":{"speed":8.5,"deg":317},"clouds":{"all":0},"dt":1486603649,"sys":{"message":0.3449,"country":"DZ","sunrise":1486621797,"sunset":1486660553},"id":2475612,"name":"Tolga","cod":200}

这是我的 javascript:

$(document).ready(function() {

// findind my latitude and longitude 
if(navigator.geolocation){

 function success(position){
 var lat = position.coords.latitude.toString();
 var long = position.coords.longitude.toString();
 $("p").html("latitude: " + lat + "<br>longitude: " + long); 

 // using the API to get the weather from those lat and long values
 $.getJSON("http://api.openweathermap.org/data/2.5/weather?lat=" + lat  + "&lon=" + long+"&appid=6a5aa3b59ebd571086cbd82be484ec8b", function(a){
    temp = a[0].main.temp.toString();
    hum = a[0].main.humidity.toString();
    press= a[0].main.pressure.toString();
    name = a[0].name;
    $("h1").html(temp);
 });
};


 };

function failure(){
  $("p").html("Not working");
  };

  navigator.geolocation.getCurrentPosition(success,failure);


});

经纬度部分运行良好,但天气 API 却不行。 任何帮助将不胜感激。

编辑:这是我的代码笔,使它更简单:https://codepen.io/tadm123/pen/OWojPx

【问题讨论】:

    标签: javascript json geolocation weather-api


    【解决方案1】:

    对我来说工作得很好。确保您的浏览器有权知道您的位置并且计算机具有 GPS。这可能不适用于桌面。

    尝试手动设置 lat 和 long 值,它应该可以工作。

    【讨论】:

      猜你喜欢
      • 2018-07-13
      • 2018-08-05
      • 2017-05-28
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      • 2017-04-05
      • 2020-08-06
      • 2018-02-09
      相关资源
      最近更新 更多