How can I pass the function of this function to a php file called geolocation via ajax?

1
var x = document.getElementById("demo");
function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else {
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}
function showPosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude +
    "<br>Longitude: " + position.coords.longitude;
}
    
asked by anonymous 28.03.2017 / 16:55

2 answers

1

I use the following script to get the geolocation every 20 seconds and send it to a PHP page to write to the database. It may not be exactly what you want, but it's a starting point.
Create the geolocation.php file to handle your data.

       var options = {
          enableHighAccuracy: true,
          timeout: 5000,
          maximumAge: 0
        };

        function success(pos) {

          var crd = pos.coords;

          console.log('Sua posição atual é:');
          console.log('Latitude : ' + crd.latitude);
          console.log('Longitude: ' + crd.longitude);
          console.log('Mais ou menos ' + crd.accuracy + ' metros.');
          console.log('Velocidade ' + crd.speed);

        $.ajax({
            type: "POST",
            url: "geolocation.php",
            data: {latitude : crd.latitude, longitude : crd.longitude, precisao : crd.accuracy}
        });

          navigator.geolocation.clearWatch(watchId);

        };

        function error(err) {
          console.warn('ERROR(' + err.code + '): ' + err.message);
        };

        var watchId;        
        watchId = navigator.geolocation.watchPosition(success, error, options);
        setInterval(function(){ watchId = navigator.geolocation.watchPosition(success, error, options); },20000);
    
28.03.2017 / 19:21
0

Complementing Pedro Augusto's answer, but using the code snippet you posted, the easiest would be to do the post in the showPosition() function, though in this case maybe it should rename it treatPosition() or something like this:

var x = document.getElementById("demo");
function getLocation() {
    if (! navigator.geolocation) {
        x.innerHTML = "Geolocation is not supported by this browser.";
    } else {
        navigator.geolocation.getCurrentPosition(function (pos) {
            x.innerHTML = "Latitude: " + position.coords.latitude +
                          "<br>Longitude: " + position.coords.longitude;
            $.ajax({
                url: "path/to/service.php",
                type: "POST",
                data: {
                    lat: position.coords.latitude,
                    long: position.coords.longitude,
                    text: x.innerHTML
                }
            });
        });
    }
}

Of course, the code of truth depends on the characteristics of the service: what it expects with respect to method, parameter names, what the URL is, what the MIME type of the request, etc.

    
28.03.2017 / 20:39