Search code examples
google-mapsgeolocationmapsgoogle-maps-markersgoogle-geolocation

Tracking geolocation of other users using google maps in real time


Since most browsers have HTML5 enabled, tracking the current device and displaying it on the map is possible, however, is there a function for a webapp based device, to update a central database on it's location?

My theory is that a user can POST a form to upload its coordinates, but this would cause it to be slightly of a hassle.

Is there a way, preferably automated, to track a user's geolocation in real time, through a web app?

Before all the downvotes come in, I would like to clarify that yes, I am able to track myself and get geolocation working on my device. What my intention is, is to track others and receive the value, something like a tracker.

edit: totally saw the downvote coming


Solution

  • If you want to track other users in real time, you have to use a websocket. For example if you are using node.js as your backend then simply use a package named socket.io, it handles all the heavy duty work for connecting one client to another client

    Example code

    ClientSide

    <script src="/socket.io/socket.io.js"></script>
    <script>
      var socket = io('http://localhost');
    
       // Maybe send some location data through a click of a button
      socket.emit('sendLocation', { latitude: 232.33, longtitude: 232323.33 });
    
    </script>
    

    Serverside

    io.on('connection', function(socket) {
       // Socket is listening waiting for the client to send something to the server
       socket.on('sendLocation', function(data) {
           // Emit to everyone that is connected via socket.io
           io.emit('sendToEveryone', data);
       });
    });
    

    So the order is

    1) Client emit

    2) Server listen then will emit it again to the targeted user or all user

    Hope it helps