-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainMaps.js
314 lines (241 loc) · 6.85 KB
/
mainMaps.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
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
var line;
var prevpos;
var user_locations = new Array(5);
var i = 0;
var mapOptions;
var curLocation;
var hiders = new Array();
var seekers = new Array();
$( document ).ready(function() {
var seeker = new Seeker(null,null);
seekers.push(seeker);
});
google.maps.event.addDomListener(window, 'load', initialize);
function Hider(currentLocation,isTagged,marker){
//this.playerName = playerName;
//this.startTime = startTime;
this.currentLocation = currentLocation;
this.isTagged = isTagged;
this.marker = marker;
}
function Seeker(currentLocation,marker){
//this.playerName = playerName;
//this.startTime = startTime;
this.currentLocation = currentLocation;
this.marker = marker;
}
function point(x,y){
this.x = x;
this.y = y;
}
function game(gameName,totalTime){
}
function initialize() {
var pos = getUserLocation();
mapOptions = {
center: new google.maps.LatLng(45, 45),
zoom: 20,
mapTypeControl: false,
streetViewControl: false
};
var map = new google.maps.Map(document.getElementById("map-canvas"),
mapOptions);
initializeLocation(map);
google.maps.event.addDomListener(map, 'click', function(event){
var pos = event.latLng;
var hider = new Hider(pos,false,addHider(pos,map));
hiders.push(hider);
//console.log(hiders[0].currentLocation);
console.log("this is K "+hider.currentLocation.A+" this is A:"+hider.currentLocation.k+ "\n");
});
window.setInterval(function(){collectData(map);},1000);
}
// Use the DOM setInterval() function to change the offset of the symbol
// at fixed intervals.
function animateCircle(){
var count = 0;
window.setInterval(function() {
count = (count + 10) % 200;
var icons = line.get('icons');
icons[0].offset = (count / 2) + '%';
line.set('icons', icons);
}, 20);
}
function addHider(location,map){
var image = {
url: 'marker_hider.png',
// This marker is 20 pixels wide by 32 pixels tall.
size: new google.maps.Size(24, 24),
scaledSize: new google.maps.Size(24,24)
// The origin for this image is 0,0
};
var marker = new google.maps.Marker({
map:map,
position:location,
icon: image
});
// var lineCoordinates = [
// new google.maps.LatLng(location.k ,location.A),
// new google.maps.LatLng(location.k*1.000001, location.A*1.000001)
// ];
// // Create the polyline and add the symbol to it via the 'icons' property.
// line = new google.maps.Polyline({
// path: lineCoordinates,
// icons: [{
// icon: image,
// offset: '100%'
// }],
// map: map
// });
// // line.setVisible(false);
// animateCircle();
//==================This is for tapping hiders===================//
google.maps.event.addListener(marker, 'click', function() {
var currentSeeker = seekers[0].currentLocation;
var currentHider = marker.position;
var p2 = new point(currentHider.A,currentHider.k);
var p1 = new point(currentSeeker.A,currentSeeker.k);
var distance = lineDistance(p1,p2)*100000;
console.log("the distance is " + distance);
if ( distance <= 15){
var infowindow = new google.maps.InfoWindow({
content :"Got You!"
});
}
else {
var infowindow = new google.maps.InfoWindow({
content :"You are too far!"
});
}
infowindow.open(map,marker);
});
return marker;
}
function addSeeker(location,map){
var image = "marker_seeker.png"
var marker = new google.maps.Marker({
map:map,
position:location,
icon: image
});
seekers[0].marker = marker;
}
function initializeLocation(map){
if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(function(position){
var pos = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude)
map.setCenter(pos);
addSeeker(pos,map);
seekers[0].currentLocation = pos;
},function(){
handleNoGeolocation(true);
});
}
else {
// Browser doesn't support Geolocation
handleNoGeolocation(false);
}
}
function getUserLocation(){
if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(positionHelper,showError);
}
else {
// Browser doesn't support Geolocation
alert("Browser doesn't support Geolocation");
}
return curLocation;
}
function positionHelper(position){
var pos = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
curLocation = pos;
}
function collectData(map){
if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(function(position){
var pos = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude)
//map.setCenter(pos);
user_locations[i] = pos;
//console.log(pos);
//console.log(pos.A);
//console.log(pos.k);
//when I == 4, we have collected 5 locations, and then refresh
if (i == 4){
refreshLocation(user_locations,map);
}
i = (i+1) % 5;
//========================================================
},showError);
}
else {
// Browser doesn't support Geolocation
alert("Browser doesn't support Geolocation");
}
}
function refreshLocation(user_locations,map){
var latitude = 0;
var longitude = 0;
for(var i = 0;i<5;i++){
latitude += user_locations[i].k;
longitude +=user_locations[i].A;
}
latitude /=5;
longitude /=5;
var pos = new google.maps.LatLng(latitude,longitude);
map.setCenter(pos);
//console.log(pos);
var image = "marker_seeker.png"
//if (prevpos != null) prevpos.setMap(null);
var marker = new google.maps.Marker({
map:map,
position:pos,
icon: image
});
seekers[0].marker.setMap(null);
seekers[0].marker = marker;
//prevpos = marker;
}
function handleNoGeolocation(errorFlag) {
if (errorFlag) {
var content = 'Error: The Geolocation service failed.';
} else {
var content = 'Error: Your browser doesn\'t support geolocation.';
}
var options = {
map: map,
position: new google.maps.LatLng(60, 105),
content: content
};
var infowindow = new google.maps.InfoWindow(options);
map.setCenter(options.position);
}
function showError(error){
switch(error.code)
{
case error.PERMISSION_DENIED:
alert("User denied the request for Geolocation.")
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML="Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML="The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML="An unknown error occurred."
break;
}
}
function lineDistance( point1, point2 )
{
var xs = 0;
var ys = 0;
xs = point2.x - point1.x;
xs = xs * xs;
ys = point2.y - point1.y;
ys = ys * ys;
return Math.sqrt( xs + ys );
}