adding a watch and update feature to live map











up vote
0
down vote

favorite












there. I have a app working in node.js that locates a user and plots their location on a leaflet map. When others connect it shows them too. It keeps their marker live as long as they move the mouse. I would like to change a couple of things but not sure where to start and a little advice would be appreciated.



1) Update the users marker when they move.
2) Keep the marker live as long as the connection is live, not as long as they are moving the mouse or moving in general as they may stop on the jorney.



I know I can use the watch feature but not sure where to add it.I will include the application.js code with is where the map locate and placing the marker code is. If there is anything else I need to add please just ask.



UPDATE : I have the watch feature working but it doesnt update the marker?
I know it is because I can alert (lat);



navigator.geolocation.watchPosition(positionSuccess, positionError, { enableHighAccuracy: true });


There is a live version of what I have running up now on https://77.68.31.152



I am not asking anyone to do this, just point me in the right direction. Like if I need to start from scratch or I can change what I already have.



The application.js is



$(function() {
// generate unique user id
var userId = Math.random().toString(16).substring(2,15);
var socket = io.connect('/');
socket.emit('little_newbie', username);
var map;


var info = $('#infobox');
var doc = $(document);

// custom marker's icon styles
var tinyIcon = L.Icon.extend({
options: {
shadowUrl: '../assets/marker-shadow.png',
iconSize: [25, 39],
iconAnchor: [12, 36],
shadowSize: [41, 41],
shadowAnchor: [12, 38],
popupAnchor: [0, -30]
}
});
var redIcon = new tinyIcon({ iconUrl: '../assets/marker-red.png' });
var yellowIcon = new tinyIcon({ iconUrl: '../assets/marker-yellow.png' });

var sentData = {};
var connects = {};
var markers = {};
var active = false;

socket.on('load:coords', function(data) {
if (!(data.id in connects)) {
setMarker(data);
}

connects[data.id] = data;
connects[data.id].updated = $.now();
});

// check whether browser supports geolocation api
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(positionSuccess, positionError, { enableHighAccuracy: true });
} else {
$('.map').text('Your browser is out of fashion, there's no geolocation!');
}

function positionSuccess(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
var acr = position.coords.accuracy;


// mark user's position
var userMarker = L.marker([lat, lng], {
icon: redIcon
});
// uncomment for static debug
// userMarker = L.marker([51.45, 30.050], { icon: redIcon });

// load leaflet map

map = L.map('map').fitWorld();

L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
maxZoom: 18,
attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
'<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
id: 'mapbox.streets'
}).addTo(map);

userMarker.addTo(map);
userMarker.bindPopup('<p>You are here ' + username + '</p>').openPopup();

var emit = $.now();
// send coords on when user is active
doc.on('mousemove', function() {
active = true;

sentData = {
id: userId,
active: active,
coords: [{
lat: lat,
lng: lng,
acr: acr,
name: username
}]
};

if ($.now() - emit > 1) {
socket.emit('send:coords', sentData);
emit = $.now();
}
});
}

doc.bind('mouseup mouseleave', function() {
active = false;
});

// showing markers for connections
function setMarker(data) {
for (var i = 0; i < data.coords.length; i++) {

var num = +$("#count").text() + 1;
$("#count").text(num);

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>' + data.coords[i].name);
markers[data.id] = marker;
}
}

// handle geolocation api errors
function positionError(error) {
var errors = {
1: 'Authorization fails', // permission denied
2: 'Can't detect your location', //position unavailable
3: 'Connection timeout' // timeout
};
showError('Error:' + errors[error.code]);
}

function showError(msg) {
info.addClass('error').text(msg);

doc.click(function() {
info.removeClass('error');
});
}

// delete inactive users every 15 sec
setInterval(function() {
for (var ident in connects){
if ($.now() - connects[ident].updated > 200000) {
var num = +$("#count").text() - 1;
$("#count").text(num);
delete connects[ident];
map.removeLayer(markers[ident]);
}
}
}, 15000);
});


Many thanks in advance










share|improve this question









New contributor




larry chambers is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    0
    down vote

    favorite












    there. I have a app working in node.js that locates a user and plots their location on a leaflet map. When others connect it shows them too. It keeps their marker live as long as they move the mouse. I would like to change a couple of things but not sure where to start and a little advice would be appreciated.



    1) Update the users marker when they move.
    2) Keep the marker live as long as the connection is live, not as long as they are moving the mouse or moving in general as they may stop on the jorney.



    I know I can use the watch feature but not sure where to add it.I will include the application.js code with is where the map locate and placing the marker code is. If there is anything else I need to add please just ask.



    UPDATE : I have the watch feature working but it doesnt update the marker?
    I know it is because I can alert (lat);



    navigator.geolocation.watchPosition(positionSuccess, positionError, { enableHighAccuracy: true });


    There is a live version of what I have running up now on https://77.68.31.152



    I am not asking anyone to do this, just point me in the right direction. Like if I need to start from scratch or I can change what I already have.



    The application.js is



    $(function() {
    // generate unique user id
    var userId = Math.random().toString(16).substring(2,15);
    var socket = io.connect('/');
    socket.emit('little_newbie', username);
    var map;


    var info = $('#infobox');
    var doc = $(document);

    // custom marker's icon styles
    var tinyIcon = L.Icon.extend({
    options: {
    shadowUrl: '../assets/marker-shadow.png',
    iconSize: [25, 39],
    iconAnchor: [12, 36],
    shadowSize: [41, 41],
    shadowAnchor: [12, 38],
    popupAnchor: [0, -30]
    }
    });
    var redIcon = new tinyIcon({ iconUrl: '../assets/marker-red.png' });
    var yellowIcon = new tinyIcon({ iconUrl: '../assets/marker-yellow.png' });

    var sentData = {};
    var connects = {};
    var markers = {};
    var active = false;

    socket.on('load:coords', function(data) {
    if (!(data.id in connects)) {
    setMarker(data);
    }

    connects[data.id] = data;
    connects[data.id].updated = $.now();
    });

    // check whether browser supports geolocation api
    if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(positionSuccess, positionError, { enableHighAccuracy: true });
    } else {
    $('.map').text('Your browser is out of fashion, there's no geolocation!');
    }

    function positionSuccess(position) {
    var lat = position.coords.latitude;
    var lng = position.coords.longitude;
    var acr = position.coords.accuracy;


    // mark user's position
    var userMarker = L.marker([lat, lng], {
    icon: redIcon
    });
    // uncomment for static debug
    // userMarker = L.marker([51.45, 30.050], { icon: redIcon });

    // load leaflet map

    map = L.map('map').fitWorld();

    L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
    maxZoom: 18,
    attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
    '<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
    'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
    id: 'mapbox.streets'
    }).addTo(map);

    userMarker.addTo(map);
    userMarker.bindPopup('<p>You are here ' + username + '</p>').openPopup();

    var emit = $.now();
    // send coords on when user is active
    doc.on('mousemove', function() {
    active = true;

    sentData = {
    id: userId,
    active: active,
    coords: [{
    lat: lat,
    lng: lng,
    acr: acr,
    name: username
    }]
    };

    if ($.now() - emit > 1) {
    socket.emit('send:coords', sentData);
    emit = $.now();
    }
    });
    }

    doc.bind('mouseup mouseleave', function() {
    active = false;
    });

    // showing markers for connections
    function setMarker(data) {
    for (var i = 0; i < data.coords.length; i++) {

    var num = +$("#count").text() + 1;
    $("#count").text(num);

    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>' + data.coords[i].name);
    markers[data.id] = marker;
    }
    }

    // handle geolocation api errors
    function positionError(error) {
    var errors = {
    1: 'Authorization fails', // permission denied
    2: 'Can't detect your location', //position unavailable
    3: 'Connection timeout' // timeout
    };
    showError('Error:' + errors[error.code]);
    }

    function showError(msg) {
    info.addClass('error').text(msg);

    doc.click(function() {
    info.removeClass('error');
    });
    }

    // delete inactive users every 15 sec
    setInterval(function() {
    for (var ident in connects){
    if ($.now() - connects[ident].updated > 200000) {
    var num = +$("#count").text() - 1;
    $("#count").text(num);
    delete connects[ident];
    map.removeLayer(markers[ident]);
    }
    }
    }, 15000);
    });


    Many thanks in advance










    share|improve this question









    New contributor




    larry chambers is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      there. I have a app working in node.js that locates a user and plots their location on a leaflet map. When others connect it shows them too. It keeps their marker live as long as they move the mouse. I would like to change a couple of things but not sure where to start and a little advice would be appreciated.



      1) Update the users marker when they move.
      2) Keep the marker live as long as the connection is live, not as long as they are moving the mouse or moving in general as they may stop on the jorney.



      I know I can use the watch feature but not sure where to add it.I will include the application.js code with is where the map locate and placing the marker code is. If there is anything else I need to add please just ask.



      UPDATE : I have the watch feature working but it doesnt update the marker?
      I know it is because I can alert (lat);



      navigator.geolocation.watchPosition(positionSuccess, positionError, { enableHighAccuracy: true });


      There is a live version of what I have running up now on https://77.68.31.152



      I am not asking anyone to do this, just point me in the right direction. Like if I need to start from scratch or I can change what I already have.



      The application.js is



      $(function() {
      // generate unique user id
      var userId = Math.random().toString(16).substring(2,15);
      var socket = io.connect('/');
      socket.emit('little_newbie', username);
      var map;


      var info = $('#infobox');
      var doc = $(document);

      // custom marker's icon styles
      var tinyIcon = L.Icon.extend({
      options: {
      shadowUrl: '../assets/marker-shadow.png',
      iconSize: [25, 39],
      iconAnchor: [12, 36],
      shadowSize: [41, 41],
      shadowAnchor: [12, 38],
      popupAnchor: [0, -30]
      }
      });
      var redIcon = new tinyIcon({ iconUrl: '../assets/marker-red.png' });
      var yellowIcon = new tinyIcon({ iconUrl: '../assets/marker-yellow.png' });

      var sentData = {};
      var connects = {};
      var markers = {};
      var active = false;

      socket.on('load:coords', function(data) {
      if (!(data.id in connects)) {
      setMarker(data);
      }

      connects[data.id] = data;
      connects[data.id].updated = $.now();
      });

      // check whether browser supports geolocation api
      if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(positionSuccess, positionError, { enableHighAccuracy: true });
      } else {
      $('.map').text('Your browser is out of fashion, there's no geolocation!');
      }

      function positionSuccess(position) {
      var lat = position.coords.latitude;
      var lng = position.coords.longitude;
      var acr = position.coords.accuracy;


      // mark user's position
      var userMarker = L.marker([lat, lng], {
      icon: redIcon
      });
      // uncomment for static debug
      // userMarker = L.marker([51.45, 30.050], { icon: redIcon });

      // load leaflet map

      map = L.map('map').fitWorld();

      L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
      maxZoom: 18,
      attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
      '<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
      'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
      id: 'mapbox.streets'
      }).addTo(map);

      userMarker.addTo(map);
      userMarker.bindPopup('<p>You are here ' + username + '</p>').openPopup();

      var emit = $.now();
      // send coords on when user is active
      doc.on('mousemove', function() {
      active = true;

      sentData = {
      id: userId,
      active: active,
      coords: [{
      lat: lat,
      lng: lng,
      acr: acr,
      name: username
      }]
      };

      if ($.now() - emit > 1) {
      socket.emit('send:coords', sentData);
      emit = $.now();
      }
      });
      }

      doc.bind('mouseup mouseleave', function() {
      active = false;
      });

      // showing markers for connections
      function setMarker(data) {
      for (var i = 0; i < data.coords.length; i++) {

      var num = +$("#count").text() + 1;
      $("#count").text(num);

      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>' + data.coords[i].name);
      markers[data.id] = marker;
      }
      }

      // handle geolocation api errors
      function positionError(error) {
      var errors = {
      1: 'Authorization fails', // permission denied
      2: 'Can't detect your location', //position unavailable
      3: 'Connection timeout' // timeout
      };
      showError('Error:' + errors[error.code]);
      }

      function showError(msg) {
      info.addClass('error').text(msg);

      doc.click(function() {
      info.removeClass('error');
      });
      }

      // delete inactive users every 15 sec
      setInterval(function() {
      for (var ident in connects){
      if ($.now() - connects[ident].updated > 200000) {
      var num = +$("#count").text() - 1;
      $("#count").text(num);
      delete connects[ident];
      map.removeLayer(markers[ident]);
      }
      }
      }, 15000);
      });


      Many thanks in advance










      share|improve this question









      New contributor




      larry chambers is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      there. I have a app working in node.js that locates a user and plots their location on a leaflet map. When others connect it shows them too. It keeps their marker live as long as they move the mouse. I would like to change a couple of things but not sure where to start and a little advice would be appreciated.



      1) Update the users marker when they move.
      2) Keep the marker live as long as the connection is live, not as long as they are moving the mouse or moving in general as they may stop on the jorney.



      I know I can use the watch feature but not sure where to add it.I will include the application.js code with is where the map locate and placing the marker code is. If there is anything else I need to add please just ask.



      UPDATE : I have the watch feature working but it doesnt update the marker?
      I know it is because I can alert (lat);



      navigator.geolocation.watchPosition(positionSuccess, positionError, { enableHighAccuracy: true });


      There is a live version of what I have running up now on https://77.68.31.152



      I am not asking anyone to do this, just point me in the right direction. Like if I need to start from scratch or I can change what I already have.



      The application.js is



      $(function() {
      // generate unique user id
      var userId = Math.random().toString(16).substring(2,15);
      var socket = io.connect('/');
      socket.emit('little_newbie', username);
      var map;


      var info = $('#infobox');
      var doc = $(document);

      // custom marker's icon styles
      var tinyIcon = L.Icon.extend({
      options: {
      shadowUrl: '../assets/marker-shadow.png',
      iconSize: [25, 39],
      iconAnchor: [12, 36],
      shadowSize: [41, 41],
      shadowAnchor: [12, 38],
      popupAnchor: [0, -30]
      }
      });
      var redIcon = new tinyIcon({ iconUrl: '../assets/marker-red.png' });
      var yellowIcon = new tinyIcon({ iconUrl: '../assets/marker-yellow.png' });

      var sentData = {};
      var connects = {};
      var markers = {};
      var active = false;

      socket.on('load:coords', function(data) {
      if (!(data.id in connects)) {
      setMarker(data);
      }

      connects[data.id] = data;
      connects[data.id].updated = $.now();
      });

      // check whether browser supports geolocation api
      if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(positionSuccess, positionError, { enableHighAccuracy: true });
      } else {
      $('.map').text('Your browser is out of fashion, there's no geolocation!');
      }

      function positionSuccess(position) {
      var lat = position.coords.latitude;
      var lng = position.coords.longitude;
      var acr = position.coords.accuracy;


      // mark user's position
      var userMarker = L.marker([lat, lng], {
      icon: redIcon
      });
      // uncomment for static debug
      // userMarker = L.marker([51.45, 30.050], { icon: redIcon });

      // load leaflet map

      map = L.map('map').fitWorld();

      L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
      maxZoom: 18,
      attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
      '<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
      'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
      id: 'mapbox.streets'
      }).addTo(map);

      userMarker.addTo(map);
      userMarker.bindPopup('<p>You are here ' + username + '</p>').openPopup();

      var emit = $.now();
      // send coords on when user is active
      doc.on('mousemove', function() {
      active = true;

      sentData = {
      id: userId,
      active: active,
      coords: [{
      lat: lat,
      lng: lng,
      acr: acr,
      name: username
      }]
      };

      if ($.now() - emit > 1) {
      socket.emit('send:coords', sentData);
      emit = $.now();
      }
      });
      }

      doc.bind('mouseup mouseleave', function() {
      active = false;
      });

      // showing markers for connections
      function setMarker(data) {
      for (var i = 0; i < data.coords.length; i++) {

      var num = +$("#count").text() + 1;
      $("#count").text(num);

      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>' + data.coords[i].name);
      markers[data.id] = marker;
      }
      }

      // handle geolocation api errors
      function positionError(error) {
      var errors = {
      1: 'Authorization fails', // permission denied
      2: 'Can't detect your location', //position unavailable
      3: 'Connection timeout' // timeout
      };
      showError('Error:' + errors[error.code]);
      }

      function showError(msg) {
      info.addClass('error').text(msg);

      doc.click(function() {
      info.removeClass('error');
      });
      }

      // delete inactive users every 15 sec
      setInterval(function() {
      for (var ident in connects){
      if ($.now() - connects[ident].updated > 200000) {
      var num = +$("#count").text() - 1;
      $("#count").text(num);
      delete connects[ident];
      map.removeLayer(markers[ident]);
      }
      }
      }, 15000);
      });


      Many thanks in advance







      javascript jquery node.js socket.io






      share|improve this question









      New contributor




      larry chambers is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      larry chambers is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited yesterday





















      New contributor




      larry chambers is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked yesterday









      larry chambers

      54




      54




      New contributor




      larry chambers is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      larry chambers is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      larry chambers is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          larry chambers is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372709%2fadding-a-watch-and-update-feature-to-live-map%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          larry chambers is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          larry chambers is a new contributor. Be nice, and check out our Code of Conduct.













          larry chambers is a new contributor. Be nice, and check out our Code of Conduct.












          larry chambers is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372709%2fadding-a-watch-and-update-feature-to-live-map%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

          Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

          A Topological Invariant for $pi_3(U(n))$