-
Notifications
You must be signed in to change notification settings - Fork 26
/
app.js
130 lines (106 loc) · 3.14 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
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
// Config
L.mapbox.accessToken = config.mapbox.accessToken;
// Utilities
function guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
}
// Get current UUID
var myUuid = localStorage.getItem('myUuid');
if (!myUuid) {
myUuid = guid();
localStorage.setItem('myUuid', myUuid);
}
// Initialize map
var map = L.mapbox.map('map', config.mapbox.mapId, {
zoomControl: false,
attributionControl: false,
tileLayer: {
maxNativeZoom: 19
}
}).setView([48.861920, 2.341755], 18)
// Stupid routing
var mapId = location.hash.replace(/^#/, '');
if (!mapId) {
mapId = (Math.random() + 1).toString(36).substring(2, 12);
location.hash = mapId;
}
// Firebase
var firebase = new Firebase('https://' + config.firebase + '.firebaseio.com/');
var markersRef = firebase.child('maps/' + mapId);
var markers = {};
function addPoint(uuid, position) {
var marker = L.marker([position.coords.latitude, position.coords.longitude], {
// zIndexOffset: (uuid === myUuid ? 1000 : 0),
icon: L.mapbox.marker.icon({
'marker-size': 'large',
'marker-color': (uuid === myUuid ? '#2196f3' : '#ff9800')
})
})
marker.addTo(map)
markers[uuid] = marker;
map.fitBounds(Object.keys(markers).map(function(uuid) {
return markers[uuid].getLatLng()
}))
}
function removePoint(uuid) {
map.removeLayer(markers[uuid])
//markers[uuid] = null
}
function updatePoint(uuid, position) {
var marker = markers[uuid]
marker.setLatLng([position.coords.latitude, position.coords.longitude])
}
function putPoint(uuid, position) {
if (markers[uuid])
updatePoint(uuid, position)
else
addPoint(uuid, position)
}
var watchPositionId;
map.on('ready', function() {
function successCoords(position) {
if (!position.coords) return
markersRef.child(myUuid).set({
coords: {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
},
timestamp: Math.floor(Date.now() / 1000)
})
// map.panTo([position.coords.latitude, position.coords.longitude])
}
function errorCoords() {
console.log('Unable to get current position')
}
watchPositionId = navigator.geolocation.watchPosition(successCoords, errorCoords);
markersRef.on('child_added', function(childSnapshot) {
var uuid = childSnapshot.key()
var position = childSnapshot.val()
addPoint(uuid, position)
})
markersRef.on('child_changed', function(childSnapshot) {
var uuid = childSnapshot.key()
var position = childSnapshot.val()
putPoint(uuid, position)
})
markersRef.on('child_removed', function(oldChildSnapshot) {
var uuid = oldChildSnapshot.key()
removePoint(uuid)
})
});
// Remove old markers
setInterval(function() {
markersRef.limitToFirst(100).once('value', function(snap) {
var now = Math.floor(Date.now() / 1000)
snap.forEach(function(childSnapshot) {
var uuid = childSnapshot.key()
if (childSnapshot.val().timestamp < now - 60 * 30) {
markersRef.child(uuid).set(null)
//markers[uuid] = null
}
})
})
}, 5000);