-
Notifications
You must be signed in to change notification settings - Fork 1
/
popular-races.js
250 lines (205 loc) · 7.26 KB
/
popular-races.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
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
var map;
var sublayer;
var tipo = 'all';
var date = 'all';
var distance = 'all';
var query;
var lat;
var lon;
function filterByType(layer) {
var sql = new cartodb.SQL({ user: 'documentation' });
var $options = $('#type_selector li');
$options.click(function(e) {
// get the area of the selected layer
var $li = $(e.target);
tipo = $li.attr('data');
// deselect all and select the clicked one
$options.removeClass('active');
$li.addClass('active');
query = getQuery();
// change the query in the layer to update the map
layer.setSQL(query);
});
}
function filterByDate(layer) {
var sql = new cartodb.SQL({ user: 'documentation' });
var $options = $('#date_selector li');
$options.click(function(e) {
// get the area of the selected layer
var $li = $(e.target);
date = $li.attr('data');
// deselect all and select the clicked one
$options.removeClass('active');
$li.addClass('active');
query = getQuery();
// change the query in the layer to update the map
layer.setSQL(query);
});
}
function filterByDistance(layer, custom) {
var sql = new cartodb.SQL({ user: 'documentation' });
if (custom) {
query = getQuery();
layer.setSQL(query);
return;
}
var $options = $('#distance_selector li');
$options.click(function(e) {
// get the area of the selected layer
var $li = $(e.target);
distance = $li.attr('data');
// deselect all and select the clicked one
$options.removeClass('active');
$li.addClass('active');
query = getQuery();
// change the query in the layer to update the map
layer.setSQL(query);
});
}
function getQuery() {
if(date !== 'all' && tipo !== 'all' && distance != 'all') {
query = getTypeAndDateAndDistanceQuery();
} else if (tipo !== 'all' && date != 'all') {
query = getTypeAndDateQuery();
} else if (tipo !== 'all' && distance != 'all') {
query = getTypeAndDistanceQuery();
} else if (date != 'all' && distance != 'all') {
query = getDateAndDistanceQuery();
} else if (tipo !== 'all') {
query = getTypeQuery();
} else if (date !== 'all') {
query = getDateQuery();
} else if (distance !== 'all') {
query = getDistanceQuery();
} else {
query = getAllDataQuery();
}
return query;
}
function getAllDataQuery() {
return "SELECT * FROM carreras_coru_u00f1a";
}
function getTypeQuery() {
return "SELECT * FROM carreras_coru_u00f1a WHERE tipo = '" + tipo + "'";
}
function getDateQuery() {
return "SELECT * FROM carreras_coru_u00f1a WHERE date between current_timestamp"
+ " AND current_timestamp + interval '" + date + "'";
}
function getDistanceQuery() {
return "SELECT * FROM carreras_coru_u00f1a"
+ " WHERE (ST_Distance(CDB_LatLng(" + lat + "," + lon + ")::geography,"
+ " the_geom::geography)/1000) <= " + distance;
}
function getTypeAndDateAndDistanceQuery() {
return "SELECT * FROM carreras_coru_u00f1a WHERE tipo = '" + tipo + "'"
+ " AND date between current_timestamp AND current_timestamp + interval '" + date + "'"
+ " AND (ST_Distance(CDB_LatLng(" + lat + "," + lon + ")::geography,"
+ " the_geom::geography)/1000) <= " + distance;
}
function getTypeAndDateQuery() {
return "SELECT * FROM carreras_coru_u00f1a WHERE tipo = '" + tipo + "'"
+ " AND date between current_timestamp AND current_timestamp + interval '" + date + "'"
}
function getTypeAndDistanceQuery() {
return "SELECT * FROM carreras_coru_u00f1a WHERE tipo = '" + tipo + "'"
+ " AND (ST_Distance(CDB_LatLng(" + lat + "," + lon + ")::geography,"
+ " the_geom::geography)/1000) <= " + distance;
}
function getDateAndDistanceQuery() {
return "SELECT * FROM carreras_coru_u00f1a WHERE date between current_timestamp"
+ " AND current_timestamp + interval '" + date + "'"
+ " AND (ST_Distance(CDB_LatLng(" + lat + "," + lon + ")::geography,"
+ " the_geom::geography)/1000) <= " + distance;
}
// credit: http://html5doctor.com/finding-your-position-with-geolocation/
function detectUserLocation(){
if (navigator.geolocation) {
var timeoutVal = 10 * 1000 * 1000;
navigator.geolocation.getCurrentPosition(
mapToPosition,
alertError,
{ enableHighAccuracy: true, timeout: timeoutVal, maximumAge: 0 }
);
}
else {
alert("Geolocation is not supported by this browser");
}
function alertError(error) {
var errors = {
1: 'Permission denied',
2: 'Position unavailable',
3: 'Request timeout'
};
alert("Error: " + errors[error.code]);
}
}
function mapToPosition(position) {
lon = position.coords.longitude;
lat = position.coords.latitude;
//map.setView(new L.LatLng(lat,lon), 14);
}
function addNewRace() {
var raceName = $('#race_name').val();
var raceDistance = $("#race_distance").val();
var raceDate = $('#race_date').val();
var raceWeb = $('#race_web').val();
var raceType = $("form input[type='radio']:checked").val();
var raceAdress = $('#race_address').val();
var raceCouncil = $('#race_council').val();
var raceProvince = $('#race_province').val();
if (raceDate.includes("/")) {
var raceDateParts = raceDate.split("/");
var dt = new Date(parseInt(raceDateParts[2], 10),
parseInt(raceDateParts[1], 10)-1,
parseInt(raceDateParts[0], 10)+1);
var dateLocale = raceDate;
} else {
var dt = new Date(raceDate);
var dateLocale = dt.toLocaleDateString();
}
var dateUTC = dt.toUTCString();
var api_key = '1c2a1d97a8027051895922a9b51573cdd52553e8';
var sql = "https://psanxiao.cartodb.com/api/v2/sql?q=INSERT INTO carreras_coru_u00f1a (the_geom, nombre, distancia, fecha, web, tipo, date)"
+ " VALUES (cdb_geocode_street_point('" + raceAdress + "', '" + raceCouncil + "', '" + raceProvince + "', 'Spain' ), '"
+ raceName + "', '" + raceDistance + "', '" + dateLocale + "', '" + raceWeb + "', '" + raceType + "', '" + dateUTC + "')&api_key=" + api_key;
$.post( sql, function( data ) {
console.log(data);
});
$('#myModalHorizontal').modal('toggle');
alert("Carrera añadida correctamente");
}
function filterByCustomDistance() {
distance = $('#custom_distance').val();
filterByDistance(sublayer, true);
}
function main() {
map = new L.Map('map', {
center: [42.91,-7.78],
zoom: 9,
https: true
})
L.tileLayer('https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png', {
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
var layerUrl = 'https://psanxiao.cartodb.com/api/v2/viz/f283f2c0-8539-11e3-a110-3085a9a9563c/viz.json';
var sublayers = [];
cartodb.createLayer(map, layerUrl)
.addTo(map)
.on('done', function(layer) {
sublayer = layer.getSubLayer(0);
sublayers.push(sublayer);
filterByType(sublayer);
filterByDate(sublayer);
filterByDistance(sublayer, false);
}).on('error', function() {
//log the error
});
L.control.locate({
strings: {
title: "Mi ubicación"
}
}).addTo(map);
}
detectUserLocation();
window.onload = main;