-
Notifications
You must be signed in to change notification settings - Fork 4
/
point_gsheets.html
163 lines (147 loc) · 5.61 KB
/
point_gsheets.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
<!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://api.tiles.mapbox.com/mapbox-gl-js/v0.37.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.37.0/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#sheeturl { position: absolute; top: 0; height: 2em; width: 100%;}
#inputurl { position: absolute; left: 0; width: 90%;}
#submiturl { position: absolute; left: 90%; width: 5%;}
#reseturl { position: absolute; left: 95%; width: 5%;}
#map { position:absolute; top:2em; bottom:20%; width:100%; }
#info { position:absolute; top:80%; height:20%; width: 100%; outline: 1px solid black; background: white; overflow: auto;}
</style>
</head>
<body>
<div id='sheeturl'>
<form>
<input id="inputurl" type="url" value="https://docs.google.com/spreadsheets/d/1sQ7a2UosYR4d1qXgSdvxw6BSuDmcOP4l6L8wLVAriQI"></input>
<input id="submiturl" type="button" value="update"></input>
<input id="reseturl" type="button" value="reset"></input>
</form>
</div>
<div id='map'></div>
<div id='info'></div>
<script>
//mapboxgl.accessToken = 'pk.eyJ1IjoiYW5uZWIiLCJhIjoiOVR1NFVoRSJ9.x0d9OLRZADt-GJaDqE0XPg';
var map = new mapboxgl.Map({
container: 'map',
style: 'styles/freetilehosting/positron.json',
zoom: 7,
center: [4.913, 52.18]
});
// First value is the default, used where there is no data
var stops = [["0", "rgba(255,0,0,0.5)"]];
// http://colorbrewer2.org/#type=sequential&scheme=OrRd&n=8
var colors=['#fff7ec','#fee8c8','#fdd49e','#fdbb84','#fc8d59','#ef6548','#d7301f','#990000'];
var dataKeyField = 'gwb_code_10'
var dataValueField = 'result';
var googleSheetKey = '1sQ7a2UosYR4d1qXgSdvxw6BSuDmcOP4l6L8wLVAriQI';
if (window.localStorage.sheetKey) {
googleSheetKey = window.localStorage.sheetKey;
}
var googleSheetName = encodeURIComponent('winkels');
var query = encodeURIComponent('select A,B,C,D');
var googleSheetUrl = `http://saturnus.geodan.nl/spreadsheets/d/${googleSheetKey}/gviz/tq?tqx=out:csv&sheet=${googleSheetName}&tq=${query}`
document.querySelector('#inputurl').value = `https://docs.google.com/spreadsheets/d/${googleSheetKey}/?sheet=${googleSheetName}`;
var BASE_URL = "https://sheets.googleapis.com/";
var SHEETAPI = "v4/spreadsheets/";
var APIKEY = "AIzaSyDk63UHzy44BUAjhOwSadxVlICDTYmct6k"
var RANGE = "A:D"
loadSheet = function(sheetid, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', BASE_URL + SHEETAPI + sheetid + "/values/" + RANGE + "?key=" + APIKEY + "");
xhr.onreadystatechange = function (event) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
callback (null, JSON.parse(xhr.responseText));
} else {
callback('Error', xhr.statusText);
}
}
};
xhr.send();
};
map.on('load', function() {
loadSheet(googleSheetKey, function(err, results) {
var data = results.values;
var features = [];
for (i = 1; i < data.length; i++) {
var feature = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [data[i][0], data[i][1]]
},
"properties": {
"title": data[i][2],
"color": data[i][3]
}
};
features.push(feature);
}
map.addSource("winkels", {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": features
}
});
map.addLayer ({
"id": "points",
"type": "circle",
"source": "winkels",
"paint" : {
"circle-color": {
"property": 'color',
"type": "identity"
},
"circle-radius": {
"base": 1.75,
"stops": [[7,2],[14,4], [22,180]]
}
}
});
map.addLayer({
"id": "labelledpoints",
"type": "symbol",
"source": "winkels",
"layout": {
//"icon-image": "circle-11",
"text-field": "{title}",
"text-font": ["Open Sans Semibold", "Arial Unicode MS Bold"],
"text-offset": [0, 0.6],
"text-anchor": "top",
}
});
});
});
map.on('mousemove', function (e) {
var features = map.queryRenderedFeatures(e.point).map(function(feature){ return {layer: {id: feature.layer.id, type: feature.layer.type}, properties:(feature.properties)};});
document.getElementById('info').innerHTML = JSON.stringify(features, null, 2);
});
document.querySelector('#submiturl').addEventListener('click', function (){
var newurl = document.querySelector('#inputurl').value;
var parts = newurl.split('/');
var keypart = 0;
var longest = 0;
for (var i = 0; i < parts.length; i++) {
if (parts[i].length > longest) {
keypart = i;
longest = parts[i].length;
}
}
window.localStorage.sheetKey = parts[keypart];
location.reload();
});
document.querySelector('#reseturl').addEventListener('click', function() {
window.localStorage.removeItem('sheetKey');
location.reload();
});
</script>
</body>
</html>