-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex2.html
65 lines (48 loc) · 1.72 KB
/
index2.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
<!DOCTYPE html>
<html>
<head>
<title>Weather API</title>
</head>
<body>
<div id="current_weather"></div>
<script src="./bower_components/jquery/dist/jquery.min.js"></script>
<script src="./bower_components/axios/dist/axios.min.js"></script>
<!--have to publish api key from http://openweathermap.org -->
<script src="./apikey.js"></script>
<script>
var region = "Seoul,kr"; // default
var current_weather = $('#current_weather'); // el target
function callApi(url){
axios.get(url)
.then(function (response) {
// console.log(response);
//console.log(response.data.weather[0].icon);
var weather_img = $('<img />', {
id: 'weather_icon_img',
src: 'http://openweathermap.org/img/w/'+response.data.weather[0].icon+'.png',
alt: ''
});
current_weather.html(weather_img);
current_weather.append('<p> Location : <strong>'+response.data.name+'</strong></p>');
current_weather.append('<p> status: <strong>'+response.data.weather[0].description+'</strong></p>');
})
.catch(function (error) {
console.log(error);
});
}
function getLocation() {
if (navigator.geolocation) {
console.log( navigator.geolocation.getCurrentPosition(showPosition) );
} else {
console.error("Geolocation is not supported by this browser.");
}
}
function showPosition(position) {
console.log("Latitude: " + position.coords.latitude + "Longitude: " + position.coords.longitude);
callApi('http://api.openweathermap.org/data/2.5/weather?appid=' + APIKEY + '&lat=' + position.coords.latitude + '&lon=' + position.coords.longitude);
}
callApi('http://api.openweathermap.org/data/2.5/weather?q='+region+'&appid=' + APIKEY);
getLocation();
</script>
</body>
</html>