Search code examples
androidgpsgeolocationandroid-location

Getting GPS co-ordinates quicker programmatically without internet, but using network provider and mobile GPS


I have referred many questions on this topic, but it seems some of my requirements are missing:

  1. I want to get the GPS location at ~0.0001 accuracy
  2. Don't want to use internet; Though GSM/CDMA network is ok
  3. Should be obtained programmatically when the app starts
  4. Should be quicker, say within a minute (like iPhone, which probably works in Airplane mode as well!)
  5. The code should work in most of the devices
  6. The phone may switch-on/off anytime and travel any distance

Is there any way to get just the co-ordinates with above mentioned requirements? Any sample code snippet will be much appreciated.

Checking various apps like "Locate Me", "Maverick", which claim to show offline locations. But in various scenarios without internet, they don't give track the location (in India).
I have referred plenty of old/new questions inside/outside SO. Below are few:

[Note: I am asking this question on behalf of my Android team and will be happy to clarify the details if asked for. Should you feel that this post belongs to Android.Stackexchange, then kindly move it.]


Solution

  • 1. I want to get the GPS location at ~0.0001 accuracy

    You can listen only to GPS provider and discard a location when it doesn't have the minimun accuracy you want:

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 5, myLocationListener);  // "myLocationListener" must be an object from a class that implements LocationListener 
    
    // "myLocationListener" implementation of LocationListener.onLocationChanged
    public void onLocationChanged(Location location)
    {    
        int MIN_ACCURACY = 5; // in metters
        if ((!location.hasAccuracy()) || (location.getAccuracy() > MIN_ACCURACY))
        {
            // discard this location and keep listening to new location readings
        }
        else
        {
            // that's a good reading!
    
            // do somethings you want... blah, blah, blah...
    
            // stop updates when you get the location to preserve users' battery.
            locationManager.removeUpdates(myLocationListener);
        }
    }
    

    2. Don't want to use internet; Though GSM/CDMA network is ok

    Yes, GPS works totally off-line. Android can make use of internet ONLY to update A-GPS data cache and provide faster reads in cold-start updates.


    3. Should be obtained programmatically when the app starts

    Then call item locationManager.requestLocationUpdates on mainActivity's onCreate event.


    4. Should be quicker, say within a minute (like iPhone, which probably works in Airplane mode as well!)

    Keep in mind that iPhone works with "high quality hardware". Android can be run on crappy devices. So it'll depend of:

    • The device's hardware quality;
    • The number of satellites that are visible in the sky at that moment;
    • The age of almanac and ephemeris data on gps cache;
    • GPS can fail to read satellites because user is inside a building or something.

    5. The code should work in most of the devices

    What is the oldest Android's API you want it to run?


    6. The phone may switch-on/off anytime and travel any distance

    I didn't get it. What is your concern about this?

    ------------------------------------------------------------------------------

    Update 1:

    "...I would like to use Android 4.0 and above..."

    I've tested GPS features from Android 2.3 to 5.0 devices. Everything runs pretty fine on all of them.

    "...I have observed that 1 geo-coordinates based demo app which was working in other devices, din't work in my LGG3 with Android 5.0. Any idea on that?..."

    Did you check GPS permissions on Android settings? (Maybe it's disabled) Or can be a hardware issue? Did you try in another similar device?

    "...Suppose the GPS is showing correct location in New York, I switch off the phone and then switch on after reaching to London, then will it still show correct location (without internet)?..."

    Sounds you're confusing things: reading a GPS location is one thing. Showing that location into a MAP is another different thing!

    You don't need to be connected to the internet to do GPS location reading. But, if you want to show that location into a MAP, probably you're gonna need internet (to load map resources, etc.).

    If you nedd to stay collecting GPS locations periodically (let's say, from 10 to 10 minutes), then it will be better to use AlarmManager to schedule a timer that will "finger" your app and say "hey, time to make a GPS reading!".

    "...Also what is your opinion about the latest Fused API?..."

    I've tested it and used it for a while, but I gave it up. It needs that Google Play Services be installed to work (not a problem, most users have it on their devices). But if you need ACCURACY (as I do), it will not work. It uses "fused sensors" (accelerometer, gps, wifi, compass, etc...) to try to get user location with minimum power possibile. Sometimes, it says you're 10 miles away from where you're really is. I couldn't make it work fine to keep the "path" where user has been. But it really saves battery.