Advertisement
Guest User

Untitled

a guest
Jan 20th, 2016
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var geocoder;
  2. window.onload = load;
  3.  
  4. function load() {
  5.    geocoder = new google.maps.Geocoder();
  6.    console.log(localStorage.weatherText);
  7.    getWeather();
  8. }
  9.  
  10. function getWeather() {
  11.    if (navigator.geolocation) {
  12.       navigator.geolocation.getCurrentPosition(loadWeatherByPos, locError);
  13.    }
  14. }
  15.  
  16. function getCityName(lat, lng) {
  17.    var latlng = new google.maps.LatLng(lat, lng);
  18.    geocoder.geocode({
  19.       'latLng': latlng
  20.    }, function (results, status) {
  21.       if (status == google.maps.GeocoderStatus.OK) {
  22.          console.log(results);
  23.          if (results[1]) {
  24.             console.log("City name: " + results[0].address_components[1].short_name + " " + results[0].address_components[1].long_name);
  25.             console.log("Province name: " + results[0].address_components[3].short_name + " " + results[0].address_components[3].long_name);
  26.          } else {
  27.             alert("No results found");
  28.          }
  29.       } else {
  30.          alert("Geocoder failed due to: " + status);
  31.       }
  32.    });
  33. }
  34.  
  35. function loadWeatherByPos(position) {
  36.    var lat = position.coords.latitude;
  37.    var lon = position.coords.longitude;
  38.    getCityName(lat, lon);
  39. }
  40.  
  41. function loadWeather(location, woeid) {
  42.    $.simpleWeather({
  43.       location: location,
  44.       woeid: woeid,
  45.       unit: 'c',
  46.       success: function (weather) {
  47.          var html = '<h2><i class="icon-' + weather.code + '"></i> ' + weather.temp + '&deg;' + weather.units.temp + '</h2>';
  48.          html += '<ul><li>' + weather.city + ', ' + weather.region + '</li>';
  49.          html += '<li class="currently">' + weather.currently + '</li>';
  50.          html += '<li>' + weather.wind.direction + ' ' + weather.wind.speed + ' ' + weather.units.speed + '</li></ul>';
  51.          $("#weather").html(html);
  52.       },
  53.       error: function (error) {
  54.          console.log(error);
  55.          $("#weather").html('<p>Error something went wrong. Check the console...?</p>');
  56.       }
  57.    });
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement