Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 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. }
  42.  
  43. $getWeather = @file_get_contents('http://api.openweathermap.org/data/2.5/weather?lat='.$lat.'&lon='.$lng.'&appid='.$appid);
  44.  
  45. function getWeather(position) {
  46.  
  47. // Get weather by your location
  48. $.getJSON("/getWeather?lat=" + position.coords.latitude + "&lon="+position.coords.longitude + "&appid=7f5806c8f3fd28b03e2d6580a50732d6", function (data) {
  49.  
  50. var celsius = Math.round(data.main.temp - 273.15);
  51. var fahrenheit = Math.round(9/5 * (data.main.temp - 273) + 32);
  52. var iconCode = data.weather[0].icon;
  53. var iconUrl = "http://openweathermap.org/img/w/" + iconCode + ".png";
  54. $(".icon").html("<img src='" + iconUrl + "'>");
  55. });
  56.  
  57. $(':radio').change(function(){
  58. // "this" will be the checked radio element
  59. if (this.id === 'celsius'){
  60. $(".showDegree").html(celsius + "&degC");
  61. }else{
  62. $(".showDegree").html(fahrenheit + "&degF");
  63. }
  64. });
  65. }
  66.  
  67. navigator.geolocation.getCurrentPosition(function(position) {
  68. getWeather(position);
  69. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement