-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal_weather.html
141 lines (104 loc) · 4.68 KB
/
local_weather.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Local Weather</title>
<link rel="stylesheet" type="text/css" href="./css/local_weather.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
// https://www.freecodecamp.com/challenges/show-the-local-weather
$(document).ready(function() {
var KEY = 'a6d9644c15004450836134101172102'; // https://developer.worldweatheronline.com/
var YAKEY = 'ADpWrVgBAAAArPCuVAIAqRO2JFE61QDGEu5q_0GeVtPd1s0AAAAAAAAAAAAs9W2isfW-RSQ3STHxzHj3Hhp2Qg==';
var text_city = 'Your City:';
var text_temp = ['Celsius', 'Fahrenheit'];
var month = {
'01':'January',
'02':'February',
'03':'March',
'04':'April',
'05':'May',
'06':'June',
'07':'July',
'08':'August',
'09':'September',
'10':'October',
'11':'November',
'12':'December'
}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition( success );
}
function success(position) {
var crd = position.coords;
var lat = Math.round(crd.latitude * 100) / 100;
var lon = Math.round(crd.longitude * 100) / 100;
//console.log( lat, lon );
$.getJSON("https://geocode-maps.yandex.ru/1.x/?geocode="+lat+","+lon+"&apikey="+YAKEY+"&format=json&sco=latlong&lang=en_US&results=1", function( json ) {
//console.log( json );
var name_city = json['response']['GeoObjectCollection']['featureMember'][0]['GeoObject']['description'];
$(".b-city__text").text( text_city );
$(".b-city__name").text( name_city );
});
$.getJSON("https://api.worldweatheronline.com/premium/v1/weather.ashx?key="+KEY+"&q="+lat+","+lon+"&format=json&num_of_days=1", function( json ) {
//console.log( json );
var date = json['data']['weather'][0]['date'].split('-');
var dateMounth = date[1];
for (var key in month) {
if (key == dateMounth) {
dateMounth = month[key];
}
}
var fullDate = date[2] + ' ' + dateMounth + ' ' + date[0];
// console.log( fullDate );
var pathData = json['data']['current_condition'][0];
var weather = pathData['weatherDesc'][0]['value'];
var iconUrl = pathData['weatherIconUrl'][0]['value'];
var tempC = pathData['temp_C'];
var tempF = pathData['temp_F'];
$('.b-city__date').text( fullDate );
$(".b-info__item:nth-child(1)").css( {'background-image': 'url(' + iconUrl + ')', 'background-repeat': 'no-repeat'} );
$('.b-info__itemTemp .b-info__text__c').text( text_temp[0] );
$('.b-info__itemTemp .b-info__text__f').text( text_temp[1] );
$(".b-info__itemTemp .b-info__c").html( tempC + '°C');
$(".b-info__itemTemp .b-info__f").html( tempF + '°F');
$(".b-info__item:nth-child(2) .b-info_itemWeather").text( weather );
$('nav a').on('click', function( e ) {
e.preventDefault();
var th = $(this);
var index = th.index();
var el = $( '.b-info__temp' );
var add = th.parent().parent().children().find( el[index] ).removeClass('hidden').addClass('block').siblings().removeClass('block').addClass('hidden');
//console.log( add );
});
});
}
});
</script>
</head>
<body>
<div class="b-weather">
<div class="b-city">
<h1><span class="b-city__text"></span> <span class="b-city__name"></span></h1>
<h2><span class="b-city__date"></span></h2>
</div>
<div class="b-info">
<div class="b-info__item">
<div class="b-info__itemTemp">
<ul>
<li class="b-info__temp b-info__c"></li>
<li class="b-info__temp hidden b-info__f"></li>
</ul>
<nav>
<a class="b-info__text__c" href=""></a>
<a class="b-info__text__f" href=""></a>
</nav>
</div>
</div>
<div class="b-info__item">
<div class="b-info_itemWeather"></div>
</div>
</div>
</div>
</body>
</html>