javascript - SimpleWeather 与地理定位不起作用

标签 javascript jquery

我正在尝试实现 SimpleWeather插入地理定位。我尝试了这段代码,但它只显示按钮。我尝试了没有地理定位的示例,它工作正常,而且我的浏览器与地理定位功能兼容。

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

我也收到此控制台错误:

Uncaught TypeError: $.simpleWeather is not a function

<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>

最佳答案

您在 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

关于javascript - SimpleWeather 与地理定位不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30079232/

相关文章:

链接 .then 时的 Jquery 延迟对象问题

javascript - 如何使用自定义参数制作自定义 CasperJS 模块?

javascript - Laravel 表单调用 javascript 函数

javascript - 检查左键或右键单击的指针事件方法

javascript - 将脚本更新为注入(inject) DOM 的新元素

javascript - 给伪元素的内容添加定位css规则

javascript - 如何在 Angularjs 中为多个选择选项分配默认值

javascript - Jquery ajax 调用在移动设备上不起作用

javascript - PeerJS 提示调用者姓名?

javascript - 给通用 slider js添加条件