2017-11-18 87 views
-1

我在FCC上构建了一个天气应用程序,我因为我的代码无法正常工作而卡住了。我没有收到浏览器访问我的位置的弹出式请求。 我在我的button.click函数中封装了该弹出式请求。 我是新手,我感到非常沮丧。我需要帮助!!

这里是我的代码:

 var Api= "https://fcc-weather-api.glitch.me/api/current?"; 

     $(document).ready(function(){ 

     $("button").click(function(){ 

     if (navigator.geolocation) { 

     navigator.geolocation.getCurrentPosition(function(position) { 
         
     var lat = "lat=" + position.coords.latitude ; 
         
     var lon = "lon=" + position.coords.longitude; 

    $.get(Api+lat+"&"+ lon, function(result){ 

     $("#city").text(result.main.name); 

     $("#country").text(result.sys.country); 

     $("#weather").text(result.weather[0].main); 

     $("#w-icon").text(result.weather[0].icon); 

     $("#temp").text(result.main.temp); 

     $("humidity").text(result.main.humidity); 

     }); 


     }); 


     }); 
      else { 

     console.log ("Geolocation is not supported by this browser"); 
    } 
     }); 

https://codepen.io/Ojomiba/pen/dVgQeQ

+0

为什么投票。当我尝试使用该网站进行学习时,我将被禁止询问更多问题。 – user8953638

回答

1

打开您的浏览器的开发者工具。

看看控制台。

阅读错误消息


Uncaught ReferenceError: lat is not defined

您已经定义lat传递给getCurrentPosition里面的功能,但是你要使用它该功能。

您需要移动试图使用它的代码,以便它在函数内。