Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法选择JSON元素_Javascript_Json_Geolocation_Weather Api - Fatal编程技术网

Javascript 无法选择JSON元素

Javascript 无法选择JSON元素,javascript,json,geolocation,weather-api,Javascript,Json,Geolocation,Weather Api,我是web开发人员的新手,但我正在尝试使用openweatherappAPI:。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":28

我是web开发人员的新手,但我正在尝试使用
openweatherapp
API:。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);


});
$(文档).ready(函数(){
//找到我的经纬度
if(导航器.地理位置){
功能成功(职位){
var lat=position.coords.lation.toString();
var long=position.coords.longitude.toString();
$(“p”).html(“纬度:“+lat+”
经度:“+long”); //使用API从这些lat和long值中获取天气信息 $.getJSON(“http://api.openweathermap.org/data/2.5/weather?lat=“+lat+”&lon=“+long+”&appid=6a5aa3b59ebd571086cbd82be484ec8b”,函数(a){ temp=a[0]。main.temp.toString(); hum=a[0].main.湿度.toString(); press=a[0]。main.pressure.toString(); name=a[0]。名称; $(“h1”).html(临时); }); }; }; 函数失效(){ $(“p”).html(“不工作”); }; navigator.geolocation.getCurrentPosition(成功,失败); });
横向和纵向部分运行良好,但不是天气的API。 任何帮助都将不胜感激


编辑:这是我的代码笔,让它更简单:

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

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