Permalink
Browse files

check if marker is loaded

  • Loading branch information...
voronianski committed Oct 7, 2012
1 parent a5a6787 commit 5f7622e3cb2a687afac56852f987eb527787927e
Showing with 56 additions and 19 deletions.
  1. +3 −2 package.json
  2. +1 −1 public/index.html
  3. +50 −14 public/js/application.js
  4. +2 −2 server.js
View
@@ -4,11 +4,12 @@
"scripts": {
"start": "server.js"
},
"version": "0.0.1-3",
"version": "0.0.1-9",
"engines": {
"node": "0.8.x"
},
"dependencies": {
"socket.io": "0.9.x"
"socket.io": "0.9.x",
"node-static": "x"
}
}
View
@@ -26,7 +26,7 @@ <h1>Realtime geolocation service with Node.js</h1>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script src="http://cdn.leafletjs.com/leaflet-0.4/leaflet.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script src="./js/application.js"></script>
View
@@ -2,9 +2,11 @@ $(function() {
// generate unique user id
var userId = Math.random().toString(16).substring(2,15);
var socket = io.connect('/');
var info = $('#infobox');
var map;
var info = $('#infobox');
//console.log(userId);
// custom marker's icon styles
var tinyIcon = L.Icon.extend({
options: {
@@ -25,20 +27,25 @@ $(function() {
}
var clients = {};
var markers = {};
socket.on('load:coords', function(id, data) {
if (!(id in clients)) {
console.log('id in clients');
clients[id] = id;
socket.on('load:coords', function(data) {
//if (!(data.id in clients)) {;
setMap(JSON.parse(data));
//}
setMarker(JSON.parse(data));
}
//clients[id].updated = $.now();
//clients[data.id] = data;
//clients[data.id].updated = $.now();
});
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(positionSuccess, positionError, { enableHighAccuracy: true });
} else {
$('.map').text('Please use modern browser, this one doesn\'t support Geolocation!');
$('.map').text('Your browser is out of fashion, there\'s no geolocation!');
}
function positionSuccess(position) {
@@ -61,27 +68,33 @@ $(function() {
// leaflet API key tiler
L.tileLayer('http://{s}.tile.cloudmade.com/BC9A493B41014CAABB98F0471D759707/997/256/{z}/{x}/{y}.png', { maxZoom: 18, detectRetina: true }).addTo(map);
map.setView([lat, lng], 2);
// map.setView([lat, lng], 6);
map.fitWorld();
userMarker.addTo(map);
userMarker.bindPopup('<p>You are there! Your ID is ' + userId + '</p>').openPopup();
$(document).bind('mousemove', function() {
resetTimer();
sentData.coords.push({
lat: lat,
lng: lng,
acr: acr
});
$('.app').click(function() { showError('Updating data..') });
socket.emit('send:coords', JSON.stringify(sentData));
//console.log('move');
socket.emit('send:coords', userId, JSON.stringify(sentData));
sentData.coords = new Array();
});
}
function setMap(data) {
var emarker;
function setMarker(data) {
//console.log('set');
for (i = 0; i < data.coords.length; i++) {
console.log(data.id);
var marker = L.marker([data.coords[i].lat, data.coords[i].lng], { icon: yellowIcon }).addTo(map);
marker.bindPopup('<p>One more external user is here!</p>');
//console.log(data.id);
emarker = L.marker([data.coords[i].lat, data.coords[i].lng], { icon: yellowIcon }).addTo(map);
emarker.bindPopup('<p>One more external user is here!</p>');
}
}
@@ -97,4 +110,27 @@ $(function() {
function showError(msg) {
info.addClass('error').text(msg);
}
// check and remove marker for inactive user
var timer = 5;
function inactivity() {
if (timer == 0) {
console.log('no activity on the page');
/*for (ident in markers) {
console.log('clean');
map.removeLayer(markers[ident]);
//console.log(clients[ident]);
}*/
resetTimer();
} else {
//console.log('else');
timer--
}
}
function resetTimer() {
timer = 5;
}
setInterval(function() { inactivity() }, 2000);
});
View
@@ -13,9 +13,9 @@ function handler(request, response) {
}
io.sockets.on('connection', function (socket) {
socket.on('send:coords', function (data) {
socket.on('send:coords', function (id, data) {
console.log(data);
socket.broadcast.emit('load:coords', data);
socket.broadcast.emit('load:coords', id, data);
});
});

0 comments on commit 5f7622e

Please sign in to comment.