2017-02-09 82 views
0

我是全新的網絡開發人員,但我嘗試使用openweatherapp API創建天氣應用程序:https://openweathermap.org/current#geo。 JSON對象是如下:無法選擇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); 


}); 

lat和長的部分運行良好但不適合天氣API。 任何幫助,將不勝感激。

編輯:這是我codepen,使其更簡單:https://codepen.io/tadm123/pen/OWojPx

回答

0

工作的罰款我。確保您的瀏覽器有權知道您的位置,並且計算機具有GPS。這可能不會在桌面上運行。

嘗試手動設置緯度和長度值,它應該工作。