-3

I have a code of getting latitude and longitude of a user. This is the code I was using to get lat and long:

function showlocation() {
    navigator.geolocation.watchPosition(callback);
}

function callback(position) {
    document.getElementById('latitude').innerHTML = position.coords.latitude;
    document.getElementById('longitude').innerHTML = position.coords.longitude;
}

I want to insert lat and long of the user every 2sec.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Hari Ram Str
  • 43
  • 1
  • 1
  • 3
  • 1
    Try to Use CRON JOB – Vignesh Chinnaiyan Oct 07 '16 at 10:32
  • @VigneshChinnaiyan Good idea, but cronjobs don't support seconds, so you need a combination of PHP timers and cronjobs – online Thomas Oct 07 '16 at 10:39
  • What server side stack are you running? You can't do it using JS alone. – Michael Westcott Oct 07 '16 at 10:39
  • Cronjob for every 2 second will suck the resources.BTW cron can be set minimum 1 min (excluding tricks) – Sudhanshu Saxena Oct 07 '16 at 10:39
  • If you read this data from client, than you should use some REST API - url, where you will post data from client to server. Backend must check whether location was changed or not (with some proximity), and if it changes - decide whether to update location of user. At frontend reasonably to use Interval for periodical action (window.setInterval()) – Akim Kelar Oct 07 '16 at 10:41

1 Answers1

0

Here is a working example, you need to play with it to fit your needs:

First: HTML/JavaScript/jQuery

index.html

<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">
    </script>
  </head>
  <body>
    <script>
      window.setInterval(function() {
        showlocation();
      }
                         , 2000);  // 2000==2 seconds
      function showlocation() {
        navigator.geolocation.watchPosition(callback);
      }
      function callback(position) {
        var lat = position.coords.latitude;
        var lon = position.coords.longitude;
        document.getElementById('latitude').innerHTML = lat;
        document.getElementById('longitude').innerHTML = lon;
        $.post('processor.php', 'lat=' + lat + '&lon=' + lon, function(response) {
          //alert(response);
        }
              );;
      }
    </script>
    Latitude: <span id="latitude"></span>
    <br/> 
    Longitude: <span id="longitude"></span>
  </body>
</html>

Second:

processor.php

    <?php
    $lat=$_POST["lat"];
    $lon=$_POST["lon"];
    // insert into database
    $conn = mysqli_connect("localhost","user","pass","database");
    $sql="INSERT INTO location_tbl (latitude, longitude) VALUES ('$lat','$lon')";  
    // You might need some conditional insert here and for every 2 seconds you will get lots of records in database!

    mysqli_query($conn,$sql) or die(mysqli_error($conn));
   // return 'done';
    ?>

This stack-overflow post could be a good reference

Note that it need user permission to share location, otherwise it won't work properly.

The solution provided is for educational purpose only...

There are many other practical ways to achieve the question's goal.

Why don't you use some PHP IP Geo Location Class/API, and re-log the Longitude, Latitude once changed?

Community
  • 1
  • 1
wpcoder
  • 437
  • 5
  • 15