-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.js
107 lines (101 loc) · 3.74 KB
/
weather.js
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
'use strict'
const getWeather = (lat, lon) => {
$.get('http://api.openweathermap.org/data/2.5/weather?lat=' + lat + '&lon=' + lon + '&units=metric' + '&appid=2de143494c0b295cca9337e1e96b00e0',
(weather) => {
let icons = {
'01d': 'wi-day-sunny',
'01n': 'wi-stars',
'02d': 'wi-day-sunny-overcast',
'02n': 'wi-night-partly-cloudy',
'03n': 'wi-night-cloudy',
'03d': 'wi-day-cloudy',
'04d': 'wi-cloudy',
'04n': 'wi-cloudy',
'09d': 'wi-day-sprinkle',
'10d': 'wi-day-rain',
'11d': 'wi-day-thunderstorm',
'13d': 'wi-day-snow',
'50d': 'wi-day-fog',
'09n': 'wi-night-sprinkle',
'10n': 'wi-night-rain',
'11n': 'wi-night-thunderstorm',
'13n': 'wi-night-snow',
'50n': 'wi-night-fog'
}
let location = weather.name + ', ' + weather.sys.country
let wind = weather.wind.speed
let humidity = weather.main.humidity
let pressure = Math.round(weather.main.pressure / 10)
let temp = Math.round(weather.main.temp * 10) / 10
let tempMax = Math.round(weather.main.temp_max * 10) / 10
let tempMin = Math.round(weather.main.temp_min * 10) / 10
let iconSource = weather.weather[0].icon
let iconName = iconSource.split(' ').map((code) => {
let results = []
results.push(icons[code])
return results.join('')
})
let icon = iconName[0]
let windIcon = 'wi-wind towards-' + weather.wind.deg + '-deg'
console.log(icon)
let sunrise = new Date(weather.sys.sunrise * 1000).toLocaleTimeString('en-au')
let sunset = new Date(weather.sys.sunset * 1000).toLocaleTimeString('en-au')
let time = new Date(weather.dt * 1000).toLocaleTimeString('en-au')
let date = new Date(weather.dt * 1000).toLocaleDateString('en-au')
$('.temp').empty().append(temp + '°C')
$('.tempMax').empty().append(tempMax + '°C')
$('.tempMin').empty().append(tempMin + '°C')
$('#temperature').removeClass().addClass('wi').addClass(icon)
$('.location').empty().append(location)
$('.time').empty().append(time)
$('.date').empty().append(date)
$('.sunrise').empty().append(sunrise)
$('.sunset').empty().append(sunset)
$('#windIcon').removeClass().addClass("wi").addClass(windIcon);
$('.wind').empty().append(wind + ' m/s')
$('.humidity').empty().append(humidity + '%')
$('.pressure').empty().append(pressure + ' kPA')
, 'json'
})
}
const getLocation = () => {
$.get('http://ip-api.com/json', (loc) => {
getWeather(loc.lat, loc.lon, loc.region)
let cityState = loc.city + loc.regionName
getPhoto(cityState, loc.lat, loc.lon)
}, 'json')
}
let getPhoto = (cityState, lat, lon) => {
let flickrApiKey = ""
$.getJSON('https://api.flickr.com/services/rest/?', {
method: "flickr.photos.search",
api_key: flickrApiKey,
text: cityState,
tags: cityState,
sort: "relevance",
format: "json",
nojsoncallback: 1
})
.done( (data) => {
$.each(data.photos.photo, (i, photo) => {
let imgCol = '.img-' + i
let image = document.createElement("IMG")
image.title = photo.title
image.setAttribute('class', 'img-responsive')
image.src = "http://farm"+photo.farm+".staticflickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret + ".jpg"
$(imgCol).html(image)
if (i === 10) {
return false
}
})
})
}
getLocation()
const customWeather = () => {
let userlat = (document.getElementById('latiude').value)
let userlong = (document.getElementById('longitude').value)
if (userlong && userlat) {
getWeather(userlat, userlong)
getPhoto(userlat, userlong)
}
}