-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
431 lines (368 loc) · 12.7 KB
/
index.html
File metadata and controls
431 lines (368 loc) · 12.7 KB
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
430
431
<!DOCTYPE html>
<html>
<head>
<title>Geolocation</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%; // padding-top: 100%;
// width: 100%; // border-radius: 50%;
// -moz-border-radius: 50%;
//-webkit-border-radius: 50%;
// border: solid 2px red;
z-index: 1; //transform: rotateZ(45deg);
}
/* Optional: Makes the sample page fill the window. */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
// Note: This example requires that you consent to location sharing when
// prompted by your browser. If you see the error "The Geolocation service
// failed.", it means you probably did not give permission for the browser to
// locate you.
var service;
var map;
var infoWindow;
var markers = [];
var pos = {
lat: -34.397,
lng: 150.644
};
function initMap() {
var options = {
// scrollwheel: false,
// navigationControl: false,
// mapTypeControl: false,
// scaleControl: false,
// draggable: false,
center: pos,
zoom: 15,
// disableDefaultUI: true,
streetViewControl: false,
// mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeId: google.maps.MapTypeId.SATELLITE,
tilt: 0,
heading: 90,
styles: [{
featureType: "poi",
//elementType: "labels",
stylers: [{
visibility: "off"
}]
}]
};
map = new google.maps.Map(document.getElementById('map'), options);
infoWindow = new google.maps.InfoWindow({
map: map
});
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
drawCurrentPos();
drawKnownPlaces();
// nearbySearch();
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
//google.maps.event.addListener(map, 'bounds_changed', function() {
google.maps.event.addListener(map, 'idle', function() {
//nearbySearch();
drawKnownPlaces();
});
}
function drawCurrentPos() {
var marker = new google.maps.Marker({
position: pos,
map: map,
icon: 'http://maps.google.com/mapfiles/ms/icons/blue-dot.png',
title: "You are here!"
});
map.setCenter(pos);
}
async function nearbySearch() {
await clearMarkers();
var request = {
location: pos,
radius: '5000'
// types: ['store']
};
service = new google.maps.places.PlacesService(map);
service.nearbySearch(request, callback);
console.log("SEARCH");
}
// Removes the markers from the map, but keeps them in the array.
function clearMarkers() {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(null);
}
markers = [];
console.log("CLEAR");
}
function callback(results, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
console.log(results);
results.forEach(createMarker);
}
}
var placeIDs = [{
id: 'ChIJNePuDBAosUcRUd83-VyI6MI',
icon: './icons/landmark.png'
},
{
id: 'ChIJzTifhbEpsUcRv_dUWHrzUA0',
icon: './icons/start.png'
},
{
id: 'ChIJ6d_ZgDImsUcRBL5oEOrb8UU',
icon: './icons/home.png'
},
{
id: 'ChIJQRQv9BkosUcRJA6c7ffoofs',
icon: './icons/finish.png'
}
];
async function drawKnownPlaces() {
await clearMarkers();
for (var i = 0; i < placeIDs.length; i++) {
var id = placeIDs[i].id;
var request = {
placeId: id
};
service = new google.maps.places.PlacesService(map);
await service.getDetails(request, callbackPlace);
}
}
function callbackPlace(place, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < placeIDs.length; i++) {
var id = placeIDs[i].id;
if (place.place_id === id) {
place.icon = placeIDs[i].icon;
}
}
createMarker(place);
} else {
console.log("ERROR: ", status);
}
}
function createViewBox() {
var viewportBox;
var bounds = map.getBounds();
var ne = bounds.getNorthEast();
var sw = bounds.getSouthWest();
var viewportPoints = [
ne, new google.maps.LatLng(ne.lat(), sw.lng()),
sw, new google.maps.LatLng(sw.lat(), ne.lng()), ne
];
/*strokeOpacity = 0 , if don't want to show the border moving. */
if (viewportBox) {
viewportBox.setPath(viewportPoints);
} else {
viewportBox = new google.maps.Polyline({
path: viewportPoints,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 4
});
//viewportBox.setMap(map);
};
return viewportBox;
}
async function createMarker(place) {
//console.log("PLACE: ", place.name);
var viewportBox = await createViewBox();
if (!google.maps.geometry.poly.containsLocation(place.geometry.location, viewportBox)) {
var padding = 0.001;
var bounds = map.getBounds();
var sw = bounds.getSouthWest();
var ne = bounds.getNorthEast();
var m = (place.geometry.location.lat() - pos.lat) / (place.geometry.location.lng() - pos.lng);
var placeLoc;
var x, y;
var compl = false;
if (ne.lng() <= place.geometry.location.lng()) {
y = m * (ne.lng() - pos.lng) + pos.lat;
x = ne.lng() - padding;
} else if (sw.lng() >= place.geometry.location.lng()) {
y = m * (sw.lng() - pos.lng) + pos.lat;
x = sw.lng() + (padding);
}
if (y <= ne.lat() && y >= sw.lat()) {
compl = true;
}
if ((!compl)) {
if (ne.lat() <= place.geometry.location.lat()) {
x = (ne.lat() + m * pos.lng - pos.lat) / m;
y = ne.lat() - padding;;
} else if (sw.lat() >= place.geometry.location.lat()) {
x = (sw.lat() + m * pos.lng - pos.lat) / m;
y = sw.lat() + padding / 2;
}
}
placeLoc = {
lat: y,
lng: x
};
var image = {
url: place.icon,
// size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(50, 50)
};
var marker = new google.maps.Marker({
map: map,
icon: image,
position: placeLoc
// label: place.name
});
markers.push(marker);
}
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: placeLoc
});
// markers.push(marker);
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(place.name);
infoWindow.open(map, this);
});
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
}
function rotate(degree) {
if (map != "undefined") {
var heading = map.getHeading() || 0;
console.log("heading" + heading);
map.setHeading(degree);
}
return;
var mapDiv = document.getElementById('map');
if (typeof mapDiv.style.transform !== "undefined") {
mapDiv.style.transform = "rotateZ(" + degree + "deg)";
} else if (typeof mapDiv.style.webkitTransform !== "undefined") {
mapDiv.style.webkitTransform = "rotateZ(" + degree + "deg)";
}
/* timer = setTimeout(function() {
rotate(++degree);
},5); */
for (var i = 0; i < markers.length; i++) {
var icon = markers[i].icon;
icon.roation = -degree;
markers[i].setIcon(icon);
}
}
// browser agnostic orientation
function getBrowserOrientation() {
var orientation;
if (screen.orientation && screen.orientation.type) {
orientation = screen.orientation.type;
} else {
orientation = screen.orientation ||
screen.mozOrientation ||
screen.msOrientation;
}
/*
'portait-primary': for (screen width < screen height, e.g. phone, phablet, small tablet)
device is in 'normal' orientation
for (screen width > screen height, e.g. large tablet, laptop)
device has been turned 90deg clockwise from normal
'portait-secondary': for (screen width < screen height)
device has been turned 180deg from normal
for (screen width > screen height)
device has been turned 90deg anti-clockwise (or 270deg clockwise) from normal
'landscape-primary': for (screen width < screen height)
device has been turned 90deg clockwise from normal
for (screen width > screen height)
device is in 'normal' orientation
'landscape-secondary': for (screen width < screen height)
device has been turned 90deg anti-clockwise (or 270deg clockwise) from normal
for (screen width > screen height)
device has been turned 180deg from normal
*/
return orientation;
}
// our current position
var positionCurrent = {
lat: null,
lng: null,
hng: null
};
var defaultOrientation;
// called on device orientation change
function onHeadingChange(event) {
console.log("WAS MACHST DU");
var heading = event.alpha;
if (typeof event.webkitCompassHeading !== "undefined") {
heading = event.webkitCompassHeading; //iOS non-standard
}
var orientation = getBrowserOrientation();
if (typeof heading !== "undefined" && heading !== null) { // && typeof orientation !== "undefined") {
// we have a browser that reports device heading and orientation
// what adjustment we have to add to rotation to allow for current device orientation
var adjustment = 0;
if (defaultOrientation === "landscape") {
adjustment -= 90;
}
if (typeof orientation !== "undefined") {
var currentOrientation = orientation.split("-");
if (defaultOrientation !== currentOrientation[0]) {
if (defaultOrientation === "landscape") {
adjustment -= 270;
} else {
adjustment -= 90;
}
}
if (currentOrientation[1] === "secondary") {
adjustment -= 180;
}
}
positionCurrent.hng = heading + adjustment;
var phase = positionCurrent.hng < 0 ? 360 + positionCurrent.hng : positionCurrent.hng;
// positionHng.textContent = (360 - phase | 0) + "°";
rotate(positionCurrent.hng);
console.log(positionCurrent.hng + "deg");
// apply rotation to compass rose
// if (typeof rose.style.transform !== "undefined") {
//rose.style.transform = "rotateZ(" + positionCurrent.hng + "deg)";
console.log(positionCurrent.hng + "deg");
// } else if (typeof rose.style.webkitTransform !== "undefined") {
//rose.style.webkitTransform = "rotateZ(" + positionCurrent.hng + "deg)";
console.log(positionCurrent.hng + "deg");
//}
} else {
// device can't show heading
// positionHng.textContent = "n/a";
//showHeadingWarning();
}
}
window.addEventListener("deviceorientation", onHeadingChange);
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDmUXs8dssxP7hTy0TCYm4brJUYJU01Vs0&libraries=places,geometry&callback=initMap">
</script>
</body>
</html>