Geolocation not re-establishing connection with satellite

You must Login before you can answer or comment on any questions.

The first time I run my app after switching the phone on, it connects to the satellite and gets the coordinates with no issue. However, every request thereafter gets the coordinates from the passive provider instead of the gps provider.

My get location function in my geolocation module looks like this:

exports.getLocation = function(purpose,callback)
{
    // Give a reason why we are using geolocation
 
    Ti.Geolocation.purpose = purpose;
 
    // Add the event listener to get the coordinates
 
    Titanium.Geolocation.addEventListener('location', callback);
}
And my callback function looks like this:
var callback = function(response)
{
    // Remove the location event listener
 
    Titanium.Geolocation.removeEventListener(response.type, callback);
 
    // Show the coords
 
    alert(response);
}
The first time I call my getLocation function, the device connects to the satellite and gets the coordinates from the gps provider. If I comment out the line that removes the event listener (in order to leave the connection to the satellite open) then every request gets the coordinates from the satellite. But I don't want to stay permanently connected to the satellite (for battery saving reasons), so I want to disconnect and then at a later stage reconnect to update the coordinates. However, as soon as I disconnect from the satellite I am unable to reconnect. Even if I exit the app and go back in, it still only uses the passive provider. I have to turn off the device in order to clear the cached coordinates before I can re-establish a connection with the satellite.

The settings I'm using for my geolocation module are as follows:

// Set default geolocation options
 
Titanium.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_BEST;
Titanium.Geolocation.preferredProvider = Titanium.Geolocation.PROVIDER_GPS;
Titanium.Geolocation.distanceFilter = 0;
Titanium.Geolocation.frequency = 1;
 
// Android specific settings
 
if (Ti.Platform.name == 'android')
{
    // According to the documentation, Android only supports up to ACCURACY_HIGH 
 
    Titanium.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_HIGH;
    Titanium.Geolocation.Android.manualMode = true;
 
    var gpsProvider = Titanium.Geolocation.Android.createLocationProvider({
        // Use a GPS satellite as the gps provider
        name : Titanium.Geolocation.PROVIDER_GPS,
        // Limit the frequency of location updates in seconds
        minUpdateTime : 60,
        // Do not send updates until the distance changes this many meters
        minUpdateDistance : 10
    });
    Titanium.Geolocation.Android.addLocationProvider(gpsProvider);
 
    var gpsRule = Titanium.Geolocation.Android.createLocationRule({
        // Rule applies to GPS provider
        provider : Titanium.Geolocation.PROVIDER_GPS,
        // Must be accurate to this value in meters
        accuracy : 10,
        // Location update should be no older than this value in milliseconds
        maxAge : 120000,
        // Location updates should be no more frequent than this value in milliseconds
        minAge : 1000 
    });
    Titanium.Geolocation.Android.addLocationRule(gpsRule);
}
Is there a way I can disable the default passive provider to force it to use gps only. If it can't connect to a satellite I want to display an error instead of using the last known location from the passive provider that could be extremely outdated and inaccurate.

As an example: First, I get the coordinates when I'm working at the office. Then (without switching the phone off to clear the coordinates) I try get the coordinates when I'm at home and it gives me the coordinates of the office. My home is at least 20km from the office, so if the distance filter and max/min age options were working then it should reconnect to the satellite? Or do these settings only apply while the connection to the satellite is active?

All I want to achieve is to be able to disconnect from the satellite and reconnect at a later stage. However, the passive provider is always used after the first disconnect from the satellite.

I am testing on Android 2.3.3 and Android 4.0.1 and both give the same result.

Any ideas would be greatly appreciated.

Many thanks.

1 Answer

I found the problem.

The first set of coordinates returned are ALWAYS, WITHOUT FAIL, the last known location. So, in the callback function, we ignore the first response and wait for the second call to the callback function - which will contain updated coordinates.

My code for the callback function now looks something like this:

var ignore = true;
 
var callback = function(response)
{   
    if (ignore == false) 
    {
        Titanium.Geolocation.removeEventListener(response.type, callback);
 
        // Show the coords
 
        alert(response);
    }
    else
    {
        ignore = false;
    }
}

Your Answer

Think you can help? Login to answer this question!