0

Im trying to send my location to server with Phonegap Build. For some reason it doesn't seem to work. What could be the reason for this and how to solve it?

Here is my js

// Wait for PhoneGap to load
//
document.addEventListener("deviceready", onDeviceReady, false);

var watchID = null;

// PhoneGap is ready
//
function onDeviceReady() {
    // Update every 3 seconds
    var options = {maximumAge:10000, enableHighAccuracy:true};
    watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
}

// onSuccess Geolocation
//

 var coords = {lat: "", lon: ""};
function onSuccess(position) {
    coords.lat = position.coords.latitude;
    coords.lon = position.coords.longitude;
}

// onError Callback receives a PositionError object
//
function onError(error) {
    alert('code: '    + error.code    + '\n' +
          'message: ' + error.message + '\n');
}


setInterval ( "Updateposition()", 15000 );
function Updateposition(){
// here you can reuse the object to send to a server
console.log("lat: " + coords.lat);
console.log("lon: " + coords.lon);
var auto = localStorage.getItem("number");

jQuery.ajax({
        type: "POST", 
        url:  serviceURL+"location.php", 
        data: 'x='+ coords.lon+'&y='+coords.lat+'&auto='+auto,
        cache: false
    });

}

and on config.xml I have

`<gap:plugin name="cordova-plugin-geolocation" source="npm" />`
user1256852
  • 53
  • 4
  • 13

1 Answers1

0

I think that navigator.geolocation.watchPosition(onSuccess, onError, options); should not be in your onDeviceReady function. the watchPosition will be called everytime the position change.

You you try to get the location onDeviceReady and then send it to the server and use navigator.geolocation.watchPosition(onSuccess, onError, options); to call your server every time the position change

hope it help you