-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtz.js
74 lines (66 loc) · 2.78 KB
/
tz.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
(function() { var geocoder; var searchtimeout; var setTime; var offsetVals = [null, null];
var getLatLng = function(addr1, addr2) {
if (!addr1 || !addr2) return;
geocoder.geocode( { 'address': addr1 }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var lat = results[0].geometry.location.lat()
var lng = results[0].geometry.location.lng()
$("#loca").text(results[0].formatted_address);
getTZ(lat,lng,0);
} else
{}//TODO: addr 1 fail
} );
geocoder.geocode( { 'address': addr2 }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var lat = results[0].geometry.location.lat()
var lng = results[0].geometry.location.lng()
$("#locb").text(results[0].formatted_address);
getTZ(lat,lng,1);
} else
{}//TODO: addr 2 fail
} );
};
var getTZ = function(lat, lng, index) {
$.getJSON("http://api.geonames.org/timezoneJSON?lat="+lat+"&lng="+lng+"&username=demo",
function(data) {
offsetVals[index] = data.dstOffset;
if (offsetVals[0] !== null && offsetVals[1] !== null) {
offset = offsetVals[1] - offsetVals[0]
now = new Date()
setTime(now, $(".time-container:first .time"))
now.setHours((now.getHours() + 24 + offset) % 24)
setTime(now, $(".time-container:last .time"))
if (offset > 0) {
offset = "+" + offset
}
$('#tztext').text(offset + " hours");
}
});
};
var setTime = function(time, elem) {
meridiem = time.getHours() <= 12 ? "AM" : "PM";
hour = time.getHours() % 12;
minute = time.getMinutes() < 10 ? "0" + time.getMinutes() : time.getMinutes();
$(elem).text(hour + ":" + minute + " " + meridiem);
}
$(document).ready(function() {
setTime(new Date(), $(".time"));
geocoder = new google.maps.Geocoder();
$('#pointa').keyup(function() {
window.clearTimeout(searchtimeout);
searchtimeout = window.setTimeout(function() {
atxt = $('#pointa').val();
btxt = $('#pointb').val();
getLatLng(atxt, btxt);
}, 500);
});
$('#pointb').keyup(function() {
window.clearTimeout(searchtimeout);
searchtimeout = window.setTimeout(function() {
atxt = $('#pointa').val();
btxt = $('#pointb').val();
getLatLng(atxt, btxt);
}, 500);
});
});
})();