Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. $(document).ready(function(){
  2.  
  3.  
  4. function getPosition() {
  5. if (navigator.geolocation) {
  6. navigator.geolocation.getCurrentPosition(getCity);
  7. } else {
  8. alert("Geolocation is not supported by this browser.");
  9. }
  10. }
  11.  
  12. function getCity(position) {
  13. var url = "https://maps.googleapis.com/maps/api/geocode/json? latlng=" + position.coords.latitude + "," + position.coords.longitude;
  14.  
  15. var city,
  16. country;
  17. $.getJSON(url, function(response) {
  18. city = response.results[0].address_components[2].short_name;
  19. country = response.results[0].address_components[5].short_name;
  20. $('.yourLocationGoesHere').attr('value', city + ", " + country);
  21. });
  22.  
  23. // Get weather by your location
  24. $.getJSON("http://api.openweathermap.org/data/2.5/weather?q=" + city + "&APPID=7f5806c8f3fd28b03e2d6580a50732d6", function (data) {
  25.  
  26. var celsius = Math.round(data.main.temp - 273.15);
  27. var fahrenheit = Math.round(9/5 * (data.main.temp - 273) + 32);
  28. var iconCode = data.weather[0].icon;
  29. var iconUrl = "http://openweathermap.org/img/w/" + iconCode + ".png";
  30. $(".icon").html("<img src='" + iconUrl + "'>");
  31. });
  32.  
  33. $(':radio').change(function(){
  34. // "this" will be the checked radio element
  35. if (this.id === 'celsius'){
  36. $(".showDegree").html(celsius + "&degC");
  37. }else{
  38. $(".showDegree").html(fahrenheit + "&degF");
  39. }
  40. });
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement