1
|
|
|
/*jslint |
2
|
|
|
indent: 4 |
3
|
|
|
*/ |
4
|
|
|
|
5
|
|
|
/*global |
6
|
|
|
$, google, window, |
7
|
|
|
Coordinates, Lang, Storage |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
var Okapi = {}; |
12
|
|
|
Okapi.m_map = null; |
13
|
|
|
Okapi.m_sites = null; |
14
|
|
|
Okapi.m_ready = false; |
15
|
|
|
Okapi.m_showCache = null; |
16
|
|
|
Okapi.m_enabled = false; |
17
|
|
|
Okapi.m_popup = null; |
18
|
|
|
Okapi.m_popupCacheCode = null; |
19
|
|
|
Okapi.m_marker = null; |
20
|
|
|
Okapi.m_icons = null; |
21
|
|
|
Okapi.m_timer = null; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
Okapi.init = function (themap) { |
25
|
|
|
'use strict'; |
26
|
|
|
|
27
|
|
|
this.m_map = themap; |
28
|
|
|
var self = this; |
29
|
|
|
|
30
|
|
|
google.maps.event.addListener(this.m_map, "center_changed", function () { |
31
|
|
|
self.scheduleLoad(); |
32
|
|
|
}); |
33
|
|
|
google.maps.event.addListener(this.m_map, "zoom_changed", function () { |
34
|
|
|
self.scheduleLoad(); |
35
|
|
|
}); |
36
|
|
|
}; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
Okapi.parseLocation = function (s) { |
40
|
|
|
'use strict'; |
41
|
|
|
|
42
|
|
|
var loc = s.split("|"), |
43
|
|
|
lat = parseFloat(loc[0]), |
44
|
|
|
lng = parseFloat(loc[1]); |
45
|
|
|
|
46
|
|
|
return Coordinates.toLatLng(lat, lng); |
47
|
|
|
}; |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
Okapi.setShowCache = function (code) { |
51
|
|
|
'use strict'; |
52
|
|
|
|
53
|
|
|
this.m_showCache = code; |
54
|
|
|
}; |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
Okapi.setupSites = function () { |
58
|
|
|
'use strict'; |
59
|
|
|
|
60
|
|
|
if (this.m_sites) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
var self = this, |
65
|
|
|
main_url = "proxy2.php?url=http://www.opencaching.de/okapi/services/apisrv/installations", |
66
|
|
|
keys = { |
67
|
|
|
"Opencaching.DE": "YSqPufH82encfJ67ZxV2", |
68
|
|
|
"Opencaching.PL": "jhRyc6rGmT6XEvxva29B", |
69
|
|
|
"Opencaching.NL": "gcwaesuq3REu8RtCgLDj", |
70
|
|
|
"Opencaching.US": "GvgyCMvwfH42GqJGL494", |
71
|
|
|
"Opencache.UK": "7t7VfpkCd4HuxPabfbHd", |
72
|
|
|
"Opencaching.RO": "gqSWmVJhZGDwc4sRhyy7" |
73
|
|
|
}, |
74
|
|
|
prefixes = { |
75
|
|
|
"Opencaching.DE": "OC", |
76
|
|
|
"Opencaching.PL": "OP", |
77
|
|
|
"Opencaching.NL": "OB", |
78
|
|
|
"Opencaching.US": "OU", |
79
|
|
|
"Opencache.UK": "OK", |
80
|
|
|
"Opencaching.RO": "OR" |
81
|
|
|
}; |
82
|
|
|
|
83
|
|
|
this.m_sites = []; |
84
|
|
|
|
85
|
|
|
$.ajax({ |
86
|
|
|
url: main_url, |
87
|
|
|
dataType: 'json', |
88
|
|
|
success: function (response) { |
89
|
|
|
response.map(function (site) { |
90
|
|
|
if (keys[site.site_name] !== undefined) { |
91
|
|
|
self.m_sites.push({ |
92
|
|
|
siteid: self.m_sites.length, |
93
|
|
|
name: site.site_name, |
94
|
|
|
site_url: site.site_url, |
95
|
|
|
url: site.okapi_base_url, |
96
|
|
|
proxy: site.okapi_base_url.startsWith('http:'), |
97
|
|
|
prefix: prefixes[site.site_name], |
98
|
|
|
key: keys[site.site_name], |
99
|
|
|
ignore_user: null, |
100
|
|
|
markers: {}, |
101
|
|
|
finished: true |
102
|
|
|
}); |
103
|
|
|
} |
104
|
|
|
}); |
105
|
|
|
|
106
|
|
|
self.m_ready = true; |
107
|
|
|
if (self.m_enabled) { |
108
|
|
|
self.scheduleLoad(true); |
109
|
|
|
} |
110
|
|
|
if (self.m_showCache && self.m_showCache !== "") { |
111
|
|
|
self.centerMap(self.m_showCache); |
112
|
|
|
self.m_showCache = null; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
}); |
116
|
|
|
}; |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
Okapi.setupIcons = function () { |
120
|
|
|
'use strict'; |
121
|
|
|
|
122
|
|
|
if (this.m_icons) { |
123
|
|
|
return; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
this.m_icons = { |
127
|
|
|
"Other": new google.maps.MarkerImage("img/cachetype-1.png"), |
128
|
|
|
"Traditional": new google.maps.MarkerImage("img/cachetype-2.png"), |
129
|
|
|
"Multi": new google.maps.MarkerImage("img/cachetype-3.png"), |
130
|
|
|
"Virtual": new google.maps.MarkerImage("img/cachetype-4.png"), |
131
|
|
|
"Webcam": new google.maps.MarkerImage("img/cachetype-5.png"), |
132
|
|
|
"Event": new google.maps.MarkerImage("img/cachetype-6.png"), |
133
|
|
|
"Quiz": new google.maps.MarkerImage("img/cachetype-7.png"), |
134
|
|
|
"Math/Physics": new google.maps.MarkerImage("img/cachetype-8.png"), |
135
|
|
|
"Moving": new google.maps.MarkerImage("img/cachetype-9.png"), |
136
|
|
|
"Drive-In": new google.maps.MarkerImage("img/cachetype-10.png") |
137
|
|
|
}; |
138
|
|
|
}; |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
Okapi.icon = function (type) { |
142
|
|
|
'use strict'; |
143
|
|
|
|
144
|
|
|
if (this.m_icons[type] !== undefined) { |
145
|
|
|
return this.m_icons[type]; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return this.m_icons.Other; |
149
|
|
|
}; |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
Okapi.guessSiteId = function (code) { |
153
|
|
|
'use strict'; |
154
|
|
|
|
155
|
|
|
code = code.toUpperCase(); |
156
|
|
|
var siteid; |
157
|
|
|
for (siteid = 0; siteid < this.m_sites.length; siteid += 1) { |
158
|
|
|
if (code.startsWith(this.m_sites[siteid].prefix)) { |
159
|
|
|
return siteid; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return -1; |
164
|
|
|
}; |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
Okapi.centerMap = function (code) { |
168
|
|
|
'use strict'; |
169
|
|
|
|
170
|
|
|
if (!this.m_ready) { |
171
|
|
|
return; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
var siteid = this.guessSiteId(code); |
175
|
|
|
if (siteid < 0) { |
176
|
|
|
return; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
this.showPopup(null, code.toUpperCase(), siteid); |
180
|
|
|
}; |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
Okapi.popupCacheCode = function () { |
184
|
|
|
'use strict'; |
185
|
|
|
|
186
|
|
|
if (!this.m_popup) { |
187
|
|
|
return null; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
var m = this.m_popup.getMap(); |
191
|
|
|
if (!m) { |
192
|
|
|
return null; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return this.m_popupCacheCode; |
196
|
|
|
}; |
197
|
|
|
|
198
|
|
|
|
199
|
|
|
Okapi.createPopupContent = function (code, response) { |
200
|
|
|
'use strict'; |
201
|
|
|
|
202
|
|
|
return '<a href="' + response.url + '" target="_blank">' + code + ' <b>' + response.name + '</b></a><br />' |
203
|
|
|
+ '<table class="cache-popup">' |
204
|
|
|
+ '<tr><td>' + Lang.t("geocache.owner") + '</td><td>' + '<a href="' + response.owner.profile_url + '" target="_blank"><b>' + response.owner.username + '</b></a></td></tr>' |
205
|
|
|
+ '<tr><td>' + Lang.t("geocache.type") + '</td><td>' + response.type + '</td></tr>' |
206
|
|
|
+ '<tr><td>' + Lang.t("geocache.size") + '</td><td>' + response.size2 + '</td></tr>' |
207
|
|
|
+ '<tr><td>' + Lang.t("geocache.status") + '</td><td>' + response.status + '</td></tr>' |
208
|
|
|
+ '<tr><td>' + Lang.t("geocache.difficulty") + '</td><td>' + response.difficulty + '/5</td></tr>' |
209
|
|
|
+ '<tr><td>' + Lang.t("geocache.terrain") + '</td><td>' + response.terrain + '/5</td></tr>' |
210
|
|
|
+ '<tr><td>' + Lang.t("geocache.finds") + '</td><td>' + response.founds + '</td></tr>' |
211
|
|
|
+ '</table>'; |
212
|
|
|
}; |
213
|
|
|
|
214
|
|
|
|
215
|
|
|
Okapi.showPopup = function (m, code, siteid) { |
216
|
|
|
'use strict'; |
217
|
|
|
|
218
|
|
|
this.m_popupCacheCode = code; |
219
|
|
|
|
220
|
|
|
if (!this.m_popup) { |
221
|
|
|
this.m_popup = new google.maps.InfoWindow(); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
var self = this, |
225
|
|
|
site = this.m_sites[siteid], |
226
|
|
|
url, |
227
|
|
|
data; |
228
|
|
|
|
229
|
|
|
url = site.url + 'services/caches/geocache'; |
230
|
|
|
data = { |
231
|
|
|
consumer_key: site.key, |
232
|
|
|
cache_code: code, |
233
|
|
|
fields: 'name|type|status|url|owner|founds|size2|difficulty|terrain|location' |
234
|
|
|
}; |
235
|
|
|
|
236
|
|
|
if (site.proxy) { |
237
|
|
|
data = { |
238
|
|
|
url: url + "?" + $.param(data) |
239
|
|
|
}; |
240
|
|
|
url = "proxy2.php"; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
$.ajax({ |
244
|
|
|
url: url, |
245
|
|
|
dataType: 'json', |
246
|
|
|
data: data, |
247
|
|
|
success: function (response) { |
248
|
|
|
var coords = self.parseLocation(response.location); |
249
|
|
|
self.m_map.setCenter(coords); |
250
|
|
|
|
251
|
|
|
if (!m) { |
252
|
|
|
m = new google.maps.Marker({ |
253
|
|
|
position: coords, |
254
|
|
|
map: self.m_map, |
255
|
|
|
icon: self.icon(response.type) |
256
|
|
|
}); |
257
|
|
|
if (self.m_maker) { |
258
|
|
|
self.m_marker.setMap(null); |
259
|
|
|
} |
260
|
|
|
self.registerPopup(m, code, siteid); |
261
|
|
|
self.m_marker = m; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
self.m_popup.setContent(self.createPopupContent(code, response)); |
265
|
|
|
self.m_popup.open(self.m_map, m); |
266
|
|
|
} |
267
|
|
|
}); |
268
|
|
|
}; |
269
|
|
|
|
270
|
|
|
|
271
|
|
|
Okapi.registerPopup = function (m, code, siteid) { |
272
|
|
|
'use strict'; |
273
|
|
|
|
274
|
|
|
if (!this.m_ready) { |
275
|
|
|
return; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
var self = this; |
279
|
|
|
|
280
|
|
|
google.maps.event.addListener(m, 'click', function () { |
281
|
|
|
self.showPopup(m, code, siteid); |
282
|
|
|
}); |
283
|
|
|
}; |
284
|
|
|
|
285
|
|
|
|
286
|
|
|
Okapi.removeMarkers = function () { |
287
|
|
|
'use strict'; |
288
|
|
|
|
289
|
|
|
if (!this.m_ready) { |
290
|
|
|
return; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
this.m_sites.map(function (site) { |
294
|
|
|
$.each(site.markers, function (_, m) { |
295
|
|
|
m.setMap(null); |
296
|
|
|
}); |
297
|
|
|
site.markers = {}; |
298
|
|
|
}); |
299
|
|
|
|
300
|
|
|
if (this.m_marker) { |
301
|
|
|
this.m_marker.setMap(null); |
302
|
|
|
delete this.m_marker; |
303
|
|
|
this.m_marker = null; |
304
|
|
|
} |
305
|
|
|
}; |
306
|
|
|
|
307
|
|
|
|
308
|
|
|
Okapi.loadBboxSite = function (siteid) { |
309
|
|
|
'use strict'; |
310
|
|
|
|
311
|
|
|
if (!this.m_ready) { |
312
|
|
|
return; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
var self = this, |
316
|
|
|
site = this.m_sites[siteid], |
317
|
|
|
b, |
318
|
|
|
bbox, |
319
|
|
|
url, |
320
|
|
|
data; |
321
|
|
|
|
322
|
|
|
if (!this.m_enabled) { |
323
|
|
|
site.finished = true; |
324
|
|
|
return; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
if (!site.finished) { |
328
|
|
|
return; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
site.finished = false; |
332
|
|
|
|
333
|
|
|
b = this.m_map.getBounds(); |
334
|
|
|
bbox = b.getSouthWest().lat() + "|" + b.getSouthWest().lng() + "|" + b.getNorthEast().lat() + "|" + b.getNorthEast().lng(); |
335
|
|
|
|
336
|
|
|
url = site.url + 'services/caches/shortcuts/search_and_retrieve'; |
337
|
|
|
data = { |
338
|
|
|
consumer_key: site.key, |
339
|
|
|
search_method: 'services/caches/search/bbox', |
340
|
|
|
search_params: '{"bbox" : "' + bbox + '", "limit" : "500"}', |
341
|
|
|
retr_method: 'services/caches/geocaches', |
342
|
|
|
retr_params: '{"fields": "code|name|location|type|status|url"}', |
343
|
|
|
wrap: 'false' |
344
|
|
|
}; |
345
|
|
|
|
346
|
|
|
if (site.proxy) { |
347
|
|
|
data = { |
348
|
|
|
url: url + "?" + $.param(data) |
349
|
|
|
}; |
350
|
|
|
url = "proxy2.php"; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
$.ajax({ |
354
|
|
|
url: url, |
355
|
|
|
dataType: 'json', |
356
|
|
|
data: data, |
357
|
|
|
success: function (response) { |
358
|
|
|
var addedCaches = {}; |
359
|
|
|
|
360
|
|
|
$.each(response, function (code, cache) { |
361
|
|
|
if (cache.status !== "Available") { |
362
|
|
|
return true; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
addedCaches[code] = true; |
366
|
|
|
if (site.markers[code] !== undefined) { |
367
|
|
|
return true; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
site.markers[code] = new google.maps.Marker({ |
371
|
|
|
position: self.parseLocation(cache.location), |
372
|
|
|
map: self.m_map, |
373
|
|
|
icon: self.icon(cache.type) |
374
|
|
|
}); |
375
|
|
|
|
376
|
|
|
self.registerPopup(site.markers[code], code, siteid); |
|
|
|
|
377
|
|
|
}); |
378
|
|
|
|
379
|
|
|
$.each(site.markers, function (code, m) { |
380
|
|
|
if (addedCaches[code] === undefined) { |
381
|
|
|
m.setMap(null); |
382
|
|
|
delete site.markers[code]; |
383
|
|
|
} |
384
|
|
|
}); |
385
|
|
|
site.finished = true; |
386
|
|
|
}, |
387
|
|
|
error: function () { |
388
|
|
|
site.markers = {}; |
389
|
|
|
site.finished = true; |
390
|
|
|
} |
391
|
|
|
}); |
392
|
|
|
}; |
393
|
|
|
|
394
|
|
|
|
395
|
|
|
Okapi.loadBbox = function () { |
396
|
|
|
'use strict'; |
397
|
|
|
|
398
|
|
|
if (!this.m_ready) { |
399
|
|
|
return; |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
var self = this; |
403
|
|
|
this.m_sites.map(function (site) { |
404
|
|
|
self.loadBboxSite(site.siteid); |
405
|
|
|
}); |
406
|
|
|
}; |
407
|
|
|
|
408
|
|
|
|
409
|
|
|
Okapi.unscheduleLoad = function () { |
410
|
|
|
'use strict'; |
411
|
|
|
|
412
|
|
|
if (!this.m_ready) { |
413
|
|
|
return; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
if (this.m_timer) { |
417
|
|
|
window.clearTimeout(this.m_timer); |
418
|
|
|
this.m_timer = null; |
419
|
|
|
} |
420
|
|
|
}; |
421
|
|
|
|
422
|
|
|
|
423
|
|
|
Okapi.scheduleLoad = function () { |
424
|
|
|
'use strict'; |
425
|
|
|
|
426
|
|
|
if (!this.m_ready) { |
427
|
|
|
return; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
var self = this; |
431
|
|
|
|
432
|
|
|
this.unscheduleLoad(); |
433
|
|
|
this.m_timer = window.setTimeout(function () { |
434
|
|
|
self.loadBbox(); |
435
|
|
|
}, 500); |
436
|
|
|
}; |
437
|
|
|
|
438
|
|
|
|
439
|
|
|
Okapi.toggle = function (t) { |
440
|
|
|
'use strict'; |
441
|
|
|
|
442
|
|
|
Storage.set('load_caches', t ? "1" : "0"); |
443
|
|
|
if ($('#geocaches').is(':checked') !== t) { |
444
|
|
|
$('#geocaches').attr('checked', t); |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
if (this.m_enabled !== t) { |
448
|
|
|
this.m_enabled = t; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
if (this.m_enabled) { |
452
|
|
|
this.setupIcons(); |
453
|
|
|
this.setupSites(); |
454
|
|
|
this.scheduleLoad(); |
455
|
|
|
} else { |
456
|
|
|
this.unscheduleLoad(); |
457
|
|
|
this.removeMarkers(); |
458
|
|
|
} |
459
|
|
|
}; |
460
|
|
|
|
461
|
|
|
|
462
|
|
|
Okapi.restore = function (defaultValue) { |
463
|
|
|
'use strict'; |
464
|
|
|
|
465
|
|
|
var state = Storage.getString("load_caches", "invalid"); |
466
|
|
|
|
467
|
|
|
if (state === "0") { |
468
|
|
|
this.toggle(false); |
469
|
|
|
} else if (state === "1") { |
470
|
|
|
this.toggle(true); |
471
|
|
|
} else { |
472
|
|
|
this.toggle(defaultValue); |
473
|
|
|
} |
474
|
|
|
}; |
475
|
|
|
|