我正在尝试实现 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+'°'+weather.units.temp+'</h2>';
html += '<ul><li>'+weather.city+', '+weather.region+'</li>';
html += '<li class="currently">'+weather.currently+'</li>';
html += '<li>'+weather.alt.temp+'°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>
关于javascript - SimpleWeather 与地理定位不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30079232/