-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
150 lines (141 loc) · 6.72 KB
/
index.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
142
143
144
145
146
147
148
149
150
<html>
<head>
<title>Weather Map</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<style>
#map {
height: 100%;
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
<script>
function locate(){
navigator.geolocation.getCurrentPosition(initialize,fail,{time:3000});
}
function initialize(position){
if(position.coords)
{
var currentLocaiton = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
}else{
var currentLocaiton = position;
}
var mapOptions = {
zoom: 7,
center: currentLocaiton,
mapTypeId: google.maps.MapTypeId.TERRAIN
};
var map = new google.maps.Map(document.getElementById('map'),mapOptions);
var infowindow = new google.maps.InfoWindow();
var infoMessage="";
var userMarker = new google.maps.Marker({
position: currentLocaiton,
map: map,
});
getWeather(currentLocaiton);
google.maps.event.addListener(map, 'click', function(event){
createMarker(event.latLng);
});
google.maps.event.addListener(userMarker,'click',function(evt){
getWeather(evt.latLng);
});
function createMarker(location) {
if (userMarker == undefined){
userMarker = new google.maps.Marker({
position: location,
map: map,
});
getWeather(location);
}
else{
userMarker.setPosition(location);
getWeather(location);
}
}
async function getTimeZone(location){
var timeZone=undefined,
key='UJ51XQSP5U1A',
url=`https://api.timezonedb.com/v2.1/get-time-zone?key=${key}&format=json&by=position&lat=${location.lat()}&lng=${location.lng()}`
await fetch(url)
.then( function(response){
return response.json();
})
.then(function (data){
if(data.status=="OK")
{//console.log(data);
timeZone= data.zoneName;
}
else
timeZone="404";
})
return timeZone;
}
function getWeather(location){
key="449de63838b85f03e45a2616932ed63c";
url=`https://api.openweathermap.org/data/2.5/weather?lat=${location.lat()}&lon=${location.lng()}&units=metric&appid=${key}`
fetch(url)
.then(function(response) {
return response.json();
})
.then( async function(data) {
var timeZone= await getTimeZone(location);
var dateString='',
dateObj=null,
zoneFailure='';
if(timeZone=="404"){
//Failed to identify the zone of the marker, then set to the default ( Browswer Time Zone)
dateObj = new Date().toLocaleString("en-US");
zoneFailure=" Error_CHKLOG";
console.log("Zone Identification failed, rendering browser local time");
}
else{
dateObj= new Date().toLocaleString("en-US", {timeZone: timeZone});
}
//For parsing date into month, day, year, reconstruct date obj
//Note : Here the time zone date will be kept but that will be treated as the browser time for the instance
dateObj= new Date(dateObj);
var month = dateObj.getUTCMonth() + 1, //months from 1-12
day = dateObj.getUTCDate(),
year = dateObj.getUTCFullYear(),
hours =dateObj.getHours(),
minutes =dateObj.getMinutes();
if(hours<12){
minutes= minutes +" AM";
}
else{
hours=hours-12;
minutes=minutes+" PM";
}
// changing accrdng to needs
dateString = day + "/" + month + "/" +year + " "+ hours + ":" + minutes;
infoMessage = `
<div styles={'font-weight':200}>
Date & Time : ${dateString+zoneFailure} <br/> <hr>
City Name : <b>${(data.name!='')?(data.name):("Unkown")}</b> <br/><hr>
Weather : <b>${data.weather[0].main}</b><br/>
Temaprature : ${data.main.temp} ° C <br/>
Humidity : ${data.main.humidity} ° C <br/>
</div>
`
//'CityName: '+data.name +'<br/> Weather: '+data.weather[0].main+'<br/> Weather Description: '+data.weather[0].description;
infowindow.close();
infowindow.setContent(infoMessage);
infowindow.open(map,userMarker);
});
}
}
function fail(){
alert('navigator.geolocation failed, may not be supported, setting default location');
initialize(new google.maps.LatLng(17.4468019, 78.3102378));
}
</script>
</head>
<body onload="locate()">
<div id="map"></div>
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
</body>
</html>