2017-04-14 18:18:20 -06:00
|
|
|
'use strict';
|
|
|
|
/* global navigator $ socket userid token mapuser toggleMaps */
|
|
|
|
|
2017-04-17 23:05:54 -06:00
|
|
|
$(function(){
|
|
|
|
|
|
|
|
var wpid, newloc;
|
|
|
|
|
|
|
|
// Set location
|
|
|
|
$('#set-loc').click(function(){
|
2017-04-25 11:44:11 -06:00
|
|
|
if (!userid===mapuser._id){ alert('You are not logged in! '); }
|
2017-04-17 23:05:54 -06:00
|
|
|
else { if (!navigator.geolocation){ alert('Geolocation not enabled. '); }
|
|
|
|
|
|
|
|
else { navigator.geolocation.getCurrentPosition(
|
|
|
|
|
|
|
|
// Success callback
|
|
|
|
function(pos){
|
2017-04-14 18:18:20 -06:00
|
|
|
var newloc = {
|
|
|
|
tok: token,
|
|
|
|
usr: userid,
|
|
|
|
lat: pos.coords.latitude,
|
|
|
|
lon: pos.coords.longitude,
|
|
|
|
spd: (pos.coords.speed||0)
|
|
|
|
};
|
|
|
|
socket.emit('set', newloc);
|
|
|
|
toggleMaps(newloc);
|
|
|
|
console.log('⚜ Set location:',newloc.lat+", "+newloc.lon);
|
2017-04-17 23:05:54 -06:00
|
|
|
},
|
|
|
|
|
|
|
|
// Error callback
|
|
|
|
function(err) {
|
2017-04-14 18:18:20 -06:00
|
|
|
alert("Unable to set location.");
|
2017-04-25 07:56:04 -06:00
|
|
|
console.error('❌️',err.message);
|
2017-04-17 23:05:54 -06:00
|
|
|
},
|
|
|
|
|
|
|
|
// Options
|
|
|
|
{ enableHighAccuracy:true }
|
|
|
|
|
|
|
|
); } }
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
// Track location
|
|
|
|
$('#track-loc').click(function(){
|
2017-04-25 11:44:11 -06:00
|
|
|
if (!userid===mapuser._id) { alert('You are not logged in! '); }
|
2017-04-17 23:05:54 -06:00
|
|
|
else {
|
|
|
|
|
|
|
|
// Start tracking
|
|
|
|
if (!wpid) {
|
|
|
|
if (!navigator.geolocation) { alert('Unable to track location. '); }
|
|
|
|
else {
|
|
|
|
$('#track-loc').html('<i class="fa fa-crosshairs fa-spin"></i> Stop').prop('title',"Click here to stop tracking your location. ");
|
|
|
|
wpid = navigator.geolocation.watchPosition(
|
|
|
|
|
|
|
|
// Success callback
|
|
|
|
function(pos) {
|
|
|
|
newloc = {
|
|
|
|
tok: token,
|
2017-04-17 23:23:21 -06:00
|
|
|
usr: userid,
|
2017-04-17 23:05:54 -06:00
|
|
|
lat: pos.coords.latitude,
|
|
|
|
lon: pos.coords.longitude,
|
|
|
|
spd: (pos.coords.speed||0)
|
|
|
|
}; socket.emit('set',newloc);
|
|
|
|
toggleMaps(newloc);
|
|
|
|
console.log('⚜ Set location:',newloc.lat+", "+newloc.lon);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Error callback
|
|
|
|
function(err){
|
|
|
|
alert("Unable to track location.");
|
|
|
|
console.error(err.message);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Options
|
|
|
|
{ enableHighAccuracy:true }
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop tracking
|
2017-04-14 18:18:20 -06:00
|
|
|
else {
|
2017-04-17 23:05:54 -06:00
|
|
|
$('#track-loc').html('<i class="fa fa-crosshairs"></i> Track').prop('title',"Click here to track your location. ");
|
|
|
|
navigator.geolocation.clearWatch(wpid);
|
|
|
|
wpid = undefined;
|
2017-04-14 18:18:20 -06:00
|
|
|
}
|
2017-04-17 23:05:54 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-14 18:18:20 -06:00
|
|
|
}
|
2017-04-17 23:05:54 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
// Clear location
|
|
|
|
$('#clear-loc').click(function(){
|
2017-04-25 11:44:11 -06:00
|
|
|
if (!userid===mapuser._id) { alert('You are not logged in! '); }
|
2017-04-17 23:05:54 -06:00
|
|
|
else {
|
|
|
|
// Stop tracking
|
|
|
|
if (wpid) {
|
|
|
|
$('#track-loc').html('<i class="fa fa-crosshairs"></i> Track');
|
|
|
|
navigator.geolocation.clearWatch(wpid);
|
|
|
|
wpid = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear location
|
|
|
|
newloc = {
|
|
|
|
tok: token,
|
|
|
|
usr: userid,
|
|
|
|
lat:0, lon:0, spd:0
|
|
|
|
}; socket.emit('set',newloc);
|
|
|
|
|
|
|
|
// Turn off map
|
|
|
|
toggleMaps(newloc);
|
|
|
|
console.log('⚜ Cleared location');
|
2017-04-14 18:18:20 -06:00
|
|
|
}
|
2017-04-17 23:05:54 -06:00
|
|
|
});
|
2017-04-14 18:18:20 -06:00
|
|
|
|
2017-04-17 23:05:54 -06:00
|
|
|
});
|