Titanium geolocation distanceFilter property is not working as expected

211 views Asked by At

Need a help here for geolocation API - even after using Titanium.Geolocation.distanceFilter = 10; 10 in meter callback function is getting triggered randomly without moving here and there. Any idea what is wrong here ?

function Geolocate() {
    var self = this;

    // The callback function we should call when location is finally determined
    this.locationReceivedCallback = function () {};
    // Check if location event is already triggered or not 
    this.isLocationEventRegister = false;
    // The function that unregisters the location event
    this.unregisterLocationEvent = function () {
        this.isLocationEventRegister = false;
        Ti.Geolocation.removeEventListener('location', self.locationReceivedCallback);
    };
}

Geolocate.prototype.init = function () {
    // Setting up some preference values

    Titanium.Geolocation.distanceFilter = 10; //The minimum change of position (in meters) before a 'location' event is fired.

    if (deviceDetect.isIos()) {
        Titanium.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_BEST; // Using this value on Android enables legacy mode operation, which is not recommended.

    } else {
        Titanium.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_LOW; //Using this value on Android enables simple mode operation.
    }
};

Geolocate.prototype.getCurrentPosition = function (callback) {
    this.isLocationEventRegister = true;
    this.locationReceivedCallback = callback;
    Titanium.Geolocation.addEventListener("location", this.locationReceivedCallback);
};

Geolocate.prototype.locationServicesAreAvailable = function () {
    if (Titanium.Geolocation.locationServicesEnabled) {
        return true;
    } else {
        return false;
    }
};

Geolocate.prototype.cancelLocationRequest = function () {
    this.unregisterLocationEvent();
};

module.exports = Geolocate;

Actual scenario is whenever I clicked on get location its gives me current location. then i tried to drag it map or image view for nearest place. Suddenly my view go current location. This is happening because of call back ? How to get rid off this ?

1

There are 1 answers

0
Rene Pot On

It's not a problem to do with your code, it is simply GPS in-accuracy.

GPS accuracy is (almost) never better than 10 meters, meaning it can be 10 meters off. When it recalculates your position it can be 10 meters down the line, so even if you're perfectly still with perfect GPS accuracy, you still might have differences of about 10 meters.

That said, you probably don't have best GPS accuracy when sitting behind a computer in a building, you probably have closer to 30-45 meters accuracy. Meaning every recalculation can easily be 10 meters differently.

Your solution would actually be rate-limit on top of using this property. The property will make sure it will not trigger multiple times per second, and then you can, in your own code, rate limit what you do with it.