-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
86 lines (74 loc) · 2.26 KB
/
app.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
var pos = document.querySelector("#pos");
var log = document.querySelector("#log");
var geoOptions = {
maximumAge: 0,
enableHighAccuracy: false
};
var j = JSON.stringify;
var map, marker, mapCenter = {lat: 47.095670, lng: 37.550172};
Notification.requestPermission(function (permission) {
// If the user accepts, let's create a notification
// if (permission === "granted") {
// var notification = new Notification("Hi there!");
// }
});
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 16,
center: mapCenter,
disableDefaultUI: true,
draggable: false,
keyboardShortcuts: false,
disableDoubleClickZoom: true,
});
map.addListener('bounds_changed', function () {
var position = marker ? marker.getPosition() : mapCenter;
map.panTo(position);
});
}
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.watchPosition(showPosition, errorPosition, geoOptions);
} else {
pos.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position, color) {
var coords = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
var latLng = new google.maps.LatLng(coords.lat, coords.lng);
if (pos.dataset.coords === j(coords)) {
return;
}
// var notification = new Notification("Location changed");
pos.dataset.coords = j(coords);
if (marker) {
marker.setPosition(latLng);
map.panTo(latLng);
} else {
marker = new google.maps.Marker({
position: latLng,
map: map,
animation: google.maps.Animation.BOUNCE
});
map.panTo(latLng);
}
var lastLoc = pos.querySelector('.loc:first-child');
if (lastLoc) {
var firstLog = log.querySelector('.loc:first-child');
// log.insertBefore(lastLoc, firstLog);
pos.removeChild(lastLoc);
}
pos.innerHTML =
"<div class='loc'>" +
"Latitude: " + coords.lat +
"<br>Longitude: " + coords.lng + '</div>';
}
function errorPosition(e) {
console.log(e);
}
navigator.serviceWorker.register('sw.js').then(function (x) {
console.log('done', x);
});