forked from rainviewer/rainviewer-api-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrainviewer-google-maps-example.html
More file actions
155 lines (127 loc) · 4.52 KB
/
rainviewer-google-maps-example.html
File metadata and controls
155 lines (127 loc) · 4.52 KB
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
151
152
153
154
<!DOCTYPE html>
<html>
<head>
<title>RainViewer API Example for Google Maps</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
li {
list-style: none;
display: inline-block;
}
</style>
</head>
<body>
<ul style="text-align:center; position: absolute;top: 0; left: 0; right: 0; height: 50px;">
<li><input type="button" onclick="stop(); showFrame(animationPosition - 1); return;" value="<" /></li>
<li><input type="button" onclick="playStop();" value="Play / Stop" /></li>
<li><input type="button" onclick="stop(); showFrame(animationPosition + 1); return;" value=">" /></li>
</ul>
<div id="timestamp" style="text-align:center; position: absolute;top: 50px; left: 0; right: 0; height: 80px;">FRAME TIME</div>
<div id="mapid" style="position: absolute; top: 80px; left: 0; bottom: 0; right: 0;"></div>
<script>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('mapid'), {
center: {lat: 40.633333, lng: -73.716667},
zoom: 7
});
/**
* Load actual radar animation frames timestamps from RainViewer API
*/
var apiRequest = new XMLHttpRequest();
apiRequest.open("GET", "https://api.rainviewer.com/public/maps.json", true);
apiRequest.onload = function(e) {
// save available timestamps and show the latest frame: "-1" means "timestamp.lenght - 1"
timestamps = JSON.parse(apiRequest.response);
showFrame(-1);
};
apiRequest.send();
}
/**
* RainViewer radar animation part
* @type {number[]}
*/
var timestamps = [];
var radarLayers = [];
var animationPosition = 0;
var animationTimer = false;
/**
* Animation functions
* @param ts
*/
function addLayer(ts) {
if (!radarLayers[ts]) {
radarLayers[ts] = new google.maps.ImageMapType({
getTileUrl: function(coord, zoom) {
return ['https://tilecache.rainviewer.com/v2/radar/' + ts + '/256/',
zoom, '/', coord.x, '/', coord.y, '/2/1_1.png'].join('');
},
tileSize: new google.maps.Size(256, 256),
opacity: 0.001
});
map.overlayMapTypes.push(radarLayers[ts]);
}
}
/**
* Display particular frame of animation for the @position
* If preloadOnly parameter is set to true, the frame layer only adds for the tiles preloading purpose
* @param position
* @param preloadOnly
*/
function changeRadarPosition(position, preloadOnly) {
while (position >= timestamps.length) {
position -= timestamps.length;
}
while (position < 0) {
position += timestamps.length;
}
var currentTimestamp = timestamps[animationPosition];
var nextTimestamp = timestamps[position];
addLayer(nextTimestamp);
if (preloadOnly) {
return;
}
animationPosition = position;
if (radarLayers[currentTimestamp]) {
radarLayers[currentTimestamp].setOpacity(0);
}
radarLayers[nextTimestamp].setOpacity(100);
document.getElementById("timestamp").innerHTML = (new Date(nextTimestamp * 1000)).toString();
}
/**
* Check avialability and show particular frame position from the timestamps list
*/
function showFrame(nextPosition) {
var preloadingDirection = nextPosition - animationPosition > 0 ? 1 : -1;
changeRadarPosition(nextPosition);
// preload next next frame (typically, +1 frame)
// if don't do that, the animation will be blinking at the first loop
changeRadarPosition(nextPosition + preloadingDirection, true);
}
/**
* Stop the animation
* Check if the animation timeout is set and clear it.
*/
function stop() {
if (animationTimer) {
clearTimeout(animationTimer);
animationTimer = false;
return true;
}
return false;
}
function play() {
showFrame(animationPosition + 1);
// Main animation driver. Run this function every 500 ms
animationTimer = setTimeout(play, 500);
}
function playStop() {
if (!stop()) {
play();
}
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY_HERE&callback=initMap" async defer></script>
</body>
</html>