加载时的simpleweather.js地理位置

时间:2018-07-04 11:52:57

标签: javascript

我是js的新手。我想让simpleweather.js加载当前位置以获取天气报告,而不是像下面的演示中那样单击onclick。

html

<div id="weather"></div>

js

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>');
   }
  });
}

0 个答案:

没有答案