How do I pass this function to a php file called Geolocation via ajax?

Asked

Viewed 81 times

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;
}
  • I am correct in assuming that by "this function", you are referring to the content of the element x? That is, the string "Latitude: XXX<br>Longitude: OOO"?

  • Yes sir, I just want to return to latitude and longitude.

2 answers

1

I use the following script to take geolocation every 20 seconds and send it to a PHP page to record to the database.
It may not be exactly what you want, but it’s a starting point.
Create the file geolocation.php to process 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);

0

Complementing the response of Pedro Augusto, but using the code snippet you posted, the easiest would be to do the post in function showPosition(), although in this case maybe she should change her name to treatPosition() or something like that:

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 truth code depends on the characteristics of the service: what it expects with respect to method, parameter names, which URL, which MIME type of the request, etc.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.