SimpleWeather与地理位置无法正常工作

时间:2015-05-06 14:10:15

标签: javascript jquery

我试图通过地理位置实施SimpleWeather。我试过这段代码,但它只是显示按钮。我尝试了没有地理位置的示例,它工作正常,而且我的浏览器兼容地理定位功能。

我在Tomcat服务器上部署文件,有什么帮助吗?

我也遇到此控制台错误:

  

未捕获的TypeError:$ .simpleWeather不是函数

<html>
<head>
<title>Weather</title>
 <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
 <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery.simpleWeather/2.5.0/jquery.simpleWeather.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js" charset="utf-8"></script>
</head>
<body>
<div id="weather"></div>
<button class="js-geolocation" style="display: none;">Use Your Location</button>

<script>
if ("geolocation" in navigator) {
  $('.js-geolocation').show(); 
} else {
  $('.js-geolocation').hide();
}

/* Where in the world are you? */
$('.js-geolocation').on('click', function() {
  navigator.geolocation.getCurrentPosition(function(position) {
    loadWeather(position.coords.latitude+','+position.coords.longitude); //load weather using your lat/lng coordinates
  });
});
$(document).ready(function() {
  loadWeather('Seattle',''); //@params location, woeid
});

function loadWeather(location, woeid) {
  $.simpleWeather({
    location: location,
    woeid: woeid,
    unit: 'f',
    success: function(weather) {
      html = '<h2><i class="icon-'+weather.code+'"></i> '+weather.temp+'&deg;'+weather.units.temp+'</h2>';
      html += '<ul><li>'+weather.city+', '+weather.region+'</li>';
      html += '<li class="currently">'+weather.currently+'</li>';
      html += '<li>'+weather.alt.temp+'&deg;C</li></ul>';  

      $("#weather").html(html);
    },
    error: function(error) {
      $("#weather").html('<p>'+error+'</p>');
    }
  });
}
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您在codepen上提供的代码不包含simpleWheather插件。将其添加到html:

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.simpleWeather/3.0.2/jquery.simpleWeather.min.js"></script>

Demo