-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
288 lines (266 loc) · 9.52 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title></title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<link href='https://fonts.googleapis.com/css?family=Roboto:300,500' rel='stylesheet' type='text/css'>
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.12.2/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.12.2/mapbox-gl.css' rel='stylesheet' />
<script src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v0.0.0/mapbox-gl-geocoder.js'></script>
<link rel='stylesheet' href='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v0.0.0/mapbox-gl-geocoder.css' type='text/css' />
<script type="text/javascript" src="https://public.tableau.com/javascripts/api/tableau-2.min.js"></script>
<style>
body {
margin: 0;
padding: 0;
}
#tableau {
position: absolute;
top: 0;
bottom: 0;
width: 50%;
/*background-color: blue*/
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 50%;
left: 50%;
height: 100%
}
pre.ui-coordinates {
position: absolute;
bottom: 10px;
right: 10px;
padding: 5px 10px;
background: rgba(0, 0, 0, 0.75);
color: #fff;
font-size: 10px;
font-family: 'Roboto', sans-serif;
font-weight: 300;
line-height: 18px;
border-radius: 3px;
z-index: 1;
}
form {
position: fixed;
bottom: 5px;
z-index: 1;
font-family: 'Roboto', sans-serif;
font-weight: 500;
font-size: 14px;
}
</style>
</head>
<body>
<div id='tableau'></div>
<div id='map'>
<pre id='coordinates' class='ui-coordinates'></pre>
<form id='toggle'>
<input type="radio" name="Speed" value=0 checked="checked"> REGULAR
<br>
<input type="radio" name="Speed" value=1> BEAST MODE
<br>
</form>
</div>
<script>
var lat = '';
var long = '';
var north = '';
var south = '';
var east = '';
var west = '';
var coordinates = document.getElementById('coordinates');
var n = 1;
var type = 0;
var latitude = '';
var longitude = '';
var workbook = '';
var worksheets = '';
var length = '';
var placeholderDiv = document.getElementById('tableau');
var url = "https://public.tableau.com/views/MapboxTest_0/QuakeDashboard?:showVizHome=no&:display_spinner=no&:jsdebug=n&:embed=y&:display_overlay=no&:display_static_image=yes&:animate_transition=no"
var options = {
hideTabs: true,
onFirstInteractive: function() {
// The viz is now ready and can be safely used.
workbook = viz.getWorkbook();
active = workbook.getActiveSheet();
worksheets = active.getWorksheets();
length = worksheets.length;
worksheets[2].applyRangeFilterAsync(
"Latitude", {
min: south,
max: north
}, tableau.FilterUpdateType.REPLACE);
worksheets[2].applyRangeFilterAsync(
"Longitude", {
min: west,
max: east
}, tableau.FilterUpdateType.REPLACE);
}
};
var viz = new tableau.Viz(placeholderDiv, url, options);
mapboxgl.accessToken = 'pk.eyJ1IjoiY210b29tZXkiLCJhIjoiUWQyeTJqMCJ9.MFgvZXmgNt6WzrzP9kJgUA';
var map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/mapbox/streets-v8', //stylesheet location
center: [0, 0], // starting position
zoom: 3 // starting zoom
});
var url = 'https://rawgit.com/cmtoomey/TableauMapboxEarthquake/master/Earthquakes.geojson';
var source = new mapboxgl.GeoJSONSource({
data: url
});
navigator.geolocation.getCurrentPosition(function(position) {
console.log(position.coords);
latitude = position.coords.latitude;
console.log(latitude);
longitude = position.coords.longitude;
console.log(longitude);
map.flyTo({
center: [longitude, latitude],
zoom: 8
});
});
map.addControl(new mapboxgl.Geocoder());
map.on('load', function() {
bounds = map.getBounds();
north = bounds.getNorth();
south = bounds.getSouth();
east = bounds.getEast();
west = bounds.getWest();
document.getElementById('coordinates').innerHTML = 'Query Count: ' + n + '<br />North: ' + north + '<br />South: ' + south + '<br />East: ' + east + '<br />West:' + west;
});
map.on('style.load', function() {
map.addSource('quake', source);
map.addLayer({
"id": "quake",
"interactive": "true",
"source": "quake",
"type": "circle",
"paint": {
"circle-color": "#A5ACAF",
"circle-radius": "7",
"circle-opacity": ".85",
"circle-blur": ".35"
}
});
});
map.on('click', function(e) {
// Use featuresAt to get features within a given radius of the click event
// Use layer option to avoid getting results from other layers
map.featuresAt(e.point, {
layer: 'quake',
radius: 10,
includeGeometry: true
}, function(err, features) {
if (err) throw err;
// if there are features within the given radius of the click event,
// fly to the location of the click event
if (features.length) {
// Get coordinates from the symbol and center the map on those coordinates
map.flyTo({
center: features[0].geometry.coordinates
});
}
});
});
var onMoveOne = function() {
n = n + 1;
bounds = map.getBounds();
north = bounds.getNorth();
south = bounds.getSouth();
east = bounds.getEast();
west = bounds.getWest();
for (var i = 0; i < length; i++) {
worksheets[i].applyRangeFilterAsync(
"Latitude", {
min: south,
max: north
}, tableau.FilterUpdateType.REPLACE);
worksheets[i].applyRangeFilterAsync(
"Longitude", {
min: west,
max: east
}, tableau.FilterUpdateType.REPLACE);
};
document.getElementById('coordinates').innerHTML = 'Query Count: ' + n + '<br />North: ' + north + '<br />South: ' + south + '<br />East: ' + east + '<br />West:' + west;
};
map.on('moveend', onMoveOne);
var onMoveMove = function() {
n = n + 1;
bounds = map.getBounds();
north = bounds.getNorth();
south = bounds.getSouth();
east = bounds.getEast();
west = bounds.getWest();
document.getElementById('coordinates').innerHTML = 'Query Count: ' + n + '<br />North: ' + north + '<br />South: ' + south + '<br />East: ' + east + '<br />West:' + west;
// This will break Tableau Public - it sends a filter update for every event as they happen. Not chained.
// it will fire an AsyncFilter for every movement of the map - with Mapbox GL...this is a lot
for (var i = 0; i < length; i++) {
worksheets[i].applyRangeFilterAsync(
"Latitude", {
min: south,
max: north
}, tableau.FilterUpdateType.REPLACE);
worksheets[i].applyRangeFilterAsync(
"Longitude", {
min: west,
max: east
}, tableau.FilterUpdateType.REPLACE);
};
};
var onMoveEnd = function() {
n = n + 1;
bounds = map.getBounds();
north = bounds.getNorth();
south = bounds.getSouth();
east = bounds.getEast();
west = bounds.getWest();
for (var i = 0; i < length; i++) {
worksheets[i].applyRangeFilterAsync(
"Latitude", {
min: south,
max: north
}, tableau.FilterUpdateType.REPLACE);
worksheets[i].applyRangeFilterAsync(
"Longitude", {
min: west,
max: east
}, tableau.FilterUpdateType.REPLACE);
};
document.getElementById('coordinates').innerHTML = 'Query Count: ' + n + '<br />North: ' + north + '<br />South: ' + south + '<br />East: ' + east + '<br />West:' + west;
};
$("input[name='Speed']").on("change", function() {
type = this.value;
if (type == 0) {
//The idea here is that the default button is set to Regular, with the event listener done on mapload.
//when you toggle back, this turns off Beast Mode and goes back to normal.
//map.off('move', onMoveMove);
map.off('moveend',onMoveEnd);
workbook.changeParameterValueAsync("Sheet Toggle", 0);
document.getElementById('toggle').style.color = '#000000';
map.setPaintProperty('water', 'fill-color', '#8ccbf7');
map.setPaintProperty('background', 'background-color', '#ede9d9');
map.setPaintProperty('admin-3-4-boundaries', 'line-color', '#c3bfcc');
map.on('moveend', onMoveEnd);
} else {
//when you toggle the button, turn off the event listener, do some other stuff, and then turn on the next one.
map.off('moveend', onMoveOne);
map.off('moveend', onMoveEnd);
workbook.changeParameterValueAsync("Sheet Toggle", 1);
document.getElementById('toggle').style.color = '#ffffff';
map.setPaintProperty('water', 'fill-color', '#002244');
map.setPaintProperty('background', 'background-color', '#69BE28');
map.setPaintProperty('admin-3-4-boundaries', 'line-color', '#A5ACAF');
//map.on('move',onMoveMove);
map.on('moveend',onMoveEnd);
}
});
</script>
</body>
</html>