Search code examples
javascriptcordovageolocationcordova-plugins

cordova geolocation does not watch or get the current location instantly


I'm using cordova 6.1.1, and I'm using the latest version of cordova-plugin-geolocation. I tried both getCurrentPossition(onSuccess,onError) and watchPossition(onSuccess, onError). And both of them are not updating my geolocation in (lat, lon) instantly.

I will track the position in getTagLocation() of my locator.js

getTagLocation: function(tagId,callback) {
        console.log('in getTagLocation func')
        locator.TrackingLocation();
},

and Tracking Location is defined as:

TrackingLocation: function() {
    var onSuccess = function(position) {
        locator.devicePosition.coords.latitude = position.coords.latitude;
        locator.devicePosition.coords.longitude = position.coords.longitude;
        locator.devicePosition.timeStamp = position.timestamp;  
    };

    var onError = function(error) {
        console.log("[ERROR]: "+error);
    };

    navigator.geolocation.watchPosition(onSuccess,onError);
},

And in my app.js, I will display the position:

displayResult: function(data){
        app.display("output message : ");
        app.display(data);
},

connectSuccess: function() {
    console.log("in connectSuccess");
    app.display("Connected to: " + app.macAddress);
    connectButton.innerHTML = "Disconnect";
    locator.getTagLocation('00176883',app.displayResult);
    setTimeout(function() {
        locator.unsubscribeTagLocation(app.displayResult);
    }, 30000);
},

From my point of view, what I did is wrapped the original function into my locator. So, in a time period of 30s, it should update my position as soon as possible. But it will not update my position around probably 5 to 6 seconds, sometimes even more than 10 seconds. I didn't find the issue trackers in their repo, so I'm asking my question here.


Solution

  • Default setting is no good, don't use that.

    What I did to change is given the optional parameters:

    navigator.geolocation.watchPosition(onSuccess,onError, {maximumAge: 300, timeout:1000, enableHighAccuracy: true});
    

    this change made the output way more instant than default settings.

    Since these values are hard coded, it really depends on your hard ware condition. If you want this watchPosition work on your devices perfectly, I would suggest you do some machine learning on that.