-
Notifications
You must be signed in to change notification settings - Fork 1
/
wave_positioner.xml
429 lines (369 loc) · 18.2 KB
/
wave_positioner.xml
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Author: Michal Aichinger, [email protected]
Copyright: CC-BY-SA
http://creativecommons.org/licenses/by-sa/3.0
-->
<Module>
<ModulePrefs title="State Example" height="400">
<Require feature="wave-preview" />
</ModulePrefs>
<Content type="html">
<![CDATA[
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="http://code.google.com/intl/cs-CZ/apis/gears/gears_init.js"></script>
<style type="text/css" media="screen">
#map {height: 300px;}
#title {margin: 10px 0 5px 0;}
#messages {font-size: 11px; height: 13px; color: #c00;}
#legend {margin-top: 3px; font-size: 12px;}
#legend img {vertical-align: bottom;}
#positionFuzzyBox{ width: 360px; background: #ccc; position: absolute; top: 200px; left: 20px; display: none;}
</style>
</head>
<body>
<div id="content">
<p id="title">Zobrazení polohy uživatelů</p>
<div id="controls">
<button id="addViewerPosition" onclick="app.addViewerPosition();">Přidej moji polohu</button>
<button id="findNearest" onclick="app.findNearest();">Najdi nejbližšího</button>
<button id="showAll" onclick="app.showAll();">Zobraz všechny</button>
</div>
<div id="messages"></div>
<div id="map"></div>
<div id="legend">
<img src="http://www.czechdesign.cz/open/gb15.png" /> moje poloha,
<img src="http://www.czechdesign.cz/open/rb15.png" /> poloha zadaná přes geolocation,
<img src="http://www.czechdesign.cz/open/yb15.png" /> poloha zadaná ručně.
</div>
<div id="positionFuzzyBox">
<p>Vaše poloha může být zaměřena až na 150m, vaší vaší polohu můžeme znepřesnit.</p>
<button id="fuzzy" onclick="app.fuzzyPosition();">Znepřesnit polohu</button>
<button id="ok" onclick="app.leavePosition();">Nechat polohu jak je</button>
<button id="close" onclick="document.getElementById('positionFuzzyBox').style.display='none';">Zavřít</button>
</div>
<script type="text/javascript">
bind = function(obj,fnc){
return function() {
return fnc.apply(obj,arguments);
}
};
function Application() {
this.map = null;
this.dom = {};
this.dom.msg = document.getElementById('messages');
this.dom.fuzzyBox = document.getElementById('positionFuzzyBox');
this.stateName = 'aichiStateHolder';
this.host = null;
this.viewer = null;
this.inited = false;
this.R = 6371; // km, prumer zeme
this.clickableMap = false;
this.marks = []; //pole vsech marku
this.infos = [];
this.init = bind(this, this.init);
this.stateUpdated = bind(this, this.stateUpdated);
this.addViewerPosition = bind(this, this.addViewerPosition);
this.setViewerPositionState = bind(this, this.setViewerPositionState);
this.findNearest = bind(this, this.findNearest);
this.showAll = bind(this, this.showAll);
this._clickedPosition = bind(this, this._clickedPosition);
this.fuzzyPosition = bind(this, this.fuzzyPosition);
this.leavePosition = bind(this, this.leavePosition);
this.showFuzzyBox = bind(this, this.showFuzzyBox);
}
/**
* init po nacteni gadgetu
*/
Application.prototype.init = function() {
//console.log('init start');
if (wave && wave.isInWaveContainer()) {
wave.setStateCallback(this.stateUpdated);
}
//praha
var lat = 50.083;
var lon = 14.467;
var latlng = new google.maps.LatLng(lat, lon);
var myOptions = {
zoom: 8,
center: latlng,
mapTypeControl: true,
navigationControl: false,
scaleControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
this.map = new google.maps.Map(document.getElementById("map"),myOptions);
google.maps.event.addListener(this.map, 'click', this._clickedPosition);
//console.log(this.map);
}
/**
* volano pokazde, kdyz se zmeni stav ve vlne
*/
Application.prototype.stateUpdated = function() {
//console.log('start updated');
this.host = wave.getHost();
this.viewer = wave.getViewer();
//zobrazeni pozic vsech
this._showPositions();
this.inited = true;
}
/**
* udalost na tlacitku
*/
Application.prototype.addViewerPosition = function() {
//console.log('addViewerPosition()');
if (navigator.geolocation) {
/* firefox geolocation is available */
//console.log('firefox');
navigator.geolocation.getCurrentPosition(this.showFuzzyBox);
} else if (google && google.gears) {
/* gears geolocation is aviable */
//console.log('gears');
var geo = google.gears.factory.create('beta.geolocation');
geo.getCurrentPosition(this.showFuzzyBox, this._showGeoLocationError);
} else {
this.dom.msg.innerHTML = 'Vyznačte svou polohu kliknutím do mapy.';
this.clickableMap = true;
}
}
Application.prototype._clickedPosition = function(event) {
if(this.clickableMap) {
this.clickableMap = false;
this.dom.msg.innerHTML = '';
var position = {};
position.latitude = event.latLng.lat();
position.longitude = event.latLng.lng();
position.id = this.viewer.getId();
position.state = 'manual';
this.setViewerPositionState(position);
}
}
/**
* zobrazeni pozice na mape
*/
Application.prototype._showPositions = function() {
//console.log('_showPositions()');
this.clearInfosAndMarks();
//zobrazeni vsech na mape
var state = this.getPositionState();
for (var i in state) {
//console.log(i);
var participant = wave.getParticipantById(state[i].id);
//participant muze byt ulozen, ale uz nemusi byt ve vlne
if (participant) {
var position = state[i];
//console.log(participant);
//nastaveni pozice na mape
var p = new google.maps.LatLng(position.latitude, position.longitude);
//zobrazuji poprve a jeste neni doinicializovano, tak vycentruji mapu na muj bod
if (!this.inited && this.viewer.getId() == participant.getId()) {
this.map.setCenter(p);
}
//zobrazeni ikony cloveka na mape
//zelene je ten kdo se diva, cervene jsou ostatni
var markerImgUrl = 'http://www.czechdesign.cz/open/rb15.png';
if (this.viewer.getId() == participant.getId()) {
markerImgUrl = 'http://www.czechdesign.cz/open/gb15.png';
} else if (position.state && position.state == 'manual'){
markerImgUrl = 'http://www.czechdesign.cz/open/yb15.png';
}
//zobrazeni puntiku
var m1 = new google.maps.Marker({
position: p,
visible: true,
title: participant.getDisplayName(),
icon: new google.maps.MarkerImage(
markerImgUrl,
new google.maps.Size(15,15,'px', 'px'),
new google.maps.Point(0,0),
new google.maps.Point(8, 8)
),
map: this.map
});
this.marks.push(m1);
var info = new Info(participant.getDisplayName(), participant.getThumbnailUrl(), m1, this.map);
this.infos.push(info);
google.maps.event.addListener(m1, 'click', info.click);
}
}
//console.log('_showPositions() done');
}
/**
* smazani starych bodu a info bublin
*/
Application.prototype.clearInfosAndMarks = function() {
for (var i = 0; i < this.infos.length; i++) {
this.infos[i].destroy;
this.infos[i] = null;
}
this.infos = [];
for (var i = 0; i < this.marks.length; i++) {
this.marks[i].setMap(null);
this.marks[i] = null;
}
this.marks = [];
}
Application.prototype._showGeoLocationError = function() {
this.dom.msg.innerHTML = 'Geolokace se nezdařila.';
}
/**
* ulozeni souradnic toho kdo to zadal a nasmerovani mapy
* na toto misto, po ulozeni pozice do state a vyvolani
* udalosti se tam nakresli tecka
*/
Application.prototype.setViewerPositionState = function(position) {
//console.log('setViewerPositionState()');
var position = position.coords || position;
//console.log('position');
//nastaveni pozice na mape
var p = new google.maps.LatLng(position.latitude, position.longitude);
this.map.setCenter(p);
//this.map.setZoom(13);
var state = this.getPositionState();
//console.log('state');
//console.log(state);
var viewer = wave.getViewer();
state[this.createName(viewer.getId())] = {id: viewer.getId(), latitude: position.latitude, longitude: position.longitude, state: (position.state) ? position.state: 'geo' };
var serializedState = wave.util.printJson(state)+'';
//console.log(serializedState);
var delta = {};
delta[this.stateName] = serializedState;
wave.getState().submitDelta(delta);
//console.log('setViewerPositionState() done');
}
/**
* ziskani stavu vsech pozic
*/
Application.prototype.getPositionState = function() {
//console.log('getPositionState()');
var value = wave.getState().get(this.stateName, '{}');
eval('var state = '+value+';');
//console.log(state);
return state;
}
/**
* vytvoreni klice z ID uzivatele
*/
Application.prototype.createName = function(name) {
return name.replace(/[^a-z]/gi, '');
}
/**
* na klik na tlacitko zobrazeni nejblizsiho souseda
*/
Application.prototype.findNearest = function() {
var state = this.getPositionState();
//zjistim vlastni polohu
var myId = this.createName(this.viewer.getId());
var myPosition = state[myId];
if (myPosition) {
var distance = Infinity;
var nearest = null;
for (var i in state) {
//nejsem ja
if (myId != i) {
var position = state[i];
var d = this._computeDistance(myPosition, position);
if (d < distance) {
nearest = state[i];
distance = d;
}
}
}
//nekoho jsem nasel
if (nearest) {
var myPos = new google.maps.LatLng(myPosition.latitude,myPosition.longitude);
var nearestPos = new google.maps.LatLng(nearest.latitude,nearest.longitude);
var llb = new google.maps.LatLngBounds(myPos, nearestPos);
this.map.fitBounds(llb);
this.map.setCenter(myPos);
this.map.setZoom(this.map.getZoom() == 1 ? 1 : this.map.getZoom()-1);
}
} else {
alert('Pro zobrazení nejbližšího, musí uložit svojí pozici.');
}
};
/**
* vypocet vzdalenosti mezi dvema body na kouli - Spherical Law of Cosines
* http://www.movable-type.co.uk/scripts/latlong.html
*/
Application.prototype._computeDistance = function(position1, position2) {
var lat1 = position1.latitude;
var lon1 = position1.longitude;
var lat2 = position2.latitude;
var lon2 = position2.longitude;
var R = this.R;
var d = Math.acos(Math.sin(lat1)*Math.sin(lat2) + Math.cos(lat1)*Math.cos(lat2) * Math.cos(lon2-lon1)) * R;
return d; //km
}
/**
* zobrazeni vsech na mape
*/
Application.prototype.showAll = function() {
var llb = new google.maps.LatLngBounds();
var state = this.getPositionState();
for (var i in state) {
var position = state[i];
llb.extend(new google.maps.LatLng(position.latitude, position.longitude));
}
if (!llb.isEmpty()) {
this.map.fitBounds(llb);
} else {
alert('Pro zobrazení všech, musí být uloženy alespoň dvě pozice.');
}
};
/**
* zobrazeni boxiku pro moznost znepresneni pozice
*/
Application.prototype.showFuzzyBox = function(position) {
this.position = position.coords || position;
this.dom.fuzzyBox.style.display = 'block';
}
/**
* znepresnuji pozici
*/
Application.prototype.fuzzyPosition = function(){
this.dom.fuzzyBox.style.display='none';
var position = {}
position.latitude = this.position.latitude + Math.random()* 0.1 * (Math.random() > 0.5 ? 1 : -1 );
position.longitude = this.position.longitude + Math.random()* 0.1 * (Math.random() > 0.5 ? 1 : -1 );
this.setViewerPositionState(position);
}
/**
* nechavam pozici namerenou tak jak je
*/
Application.prototype.leavePosition = function(){
this.dom.fuzzyBox.style.display='none';
this.setViewerPositionState(this.position);
}
/**
* @class Info
* objekt tvorici vizitku po kliku na bod
*/
function Info (name, src, mark, map) {
this.name = name;
this.src = src;
this.mark = mark;
this.map = map;
this.click = bind(this, this.click);
}
Info.prototype.click = function() {
var i = new google.maps.InfoWindow({
content: '<div><strong>'+this.name+'</strong></div><img src="'+this.src+'" />'
});
i.open(this.map, this.mark);
}
Info.prototype.destroy = function() {
for (p in this) {
this[p] = null;
}
}
var app = new Application();
gadgets.util.registerOnLoadHandler(app.init);
</script>
</div>
</body>
]]>
</Content>
</Module>