23

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.]

Community
  • 1
  • 1
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • May be you will not get that accuracy using network. As till I know GPS is hardware dependable and device is also responsible for getting coordinates slower. GPS also depend on where you are right now. Is this you consider as your requirements. – Codelord Apr 27 '15 at 09:06
  • @CodeLord, I din't get your question properly, but AFAIK, iOS is also depending on A-GPS, but still find geo-codes faster in offline mode. Yes, GPS might depends on the current location and possibly that's why we find "Last Known Loction" in android while finding the current location. All my requirements are listed in the question itself. – iammilind Apr 27 '15 at 09:12
  • 1
    With only GPS, line-of-sight is always going to be an issue. If the app is launched when inside a building/room, you are less likely to obtain a location, leave aside with the accuracy mentioned. You can find a snippet in [Android obtain location using GPS, network](http://stackoverflow.com/questions/23962769/android-gps-incorrect-location-data-on-query/24074771#24074771) . That code will work on most devices but time in which loc is obtained and accuracy would be an issue unless you are out in the open. Didn't really understand your 6th point. – Pararth Apr 29 '15 at 15:20
  • Dose GPS need internet? definitely you can get it programmatically. But get accuracy in ~0.0001, is not available by mobile – Xcihnegn Apr 29 '15 at 17:58
  • @xcihnegn, look at 2nd requirement. What level of accuracy can we get without internet? – iammilind Apr 30 '15 at 01:20
  • Go to this doc [How Accurate is the GPS on my Smart Phone?](http://communityhealthmaps.nlm.nih.gov/2014/07/07/how-accurate-is-the-gps-on-my-smart-phone-part-2/) – Xcihnegn Apr 30 '15 at 05:30

3 Answers3

11

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.

Christian
  • 7,062
  • 9
  • 53
  • 79
  • Regarding points: (5) I would like to use Android 4.0 and above. 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? (6) 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)? If not then how about nearby cities of New York? ... Also what is your opinion about the latest Fused API? – iammilind May 06 '15 at 05:58
  • I have a lot of experience with GPS and maps, from a research I did to implement it in my app. I'll be glad to help you in any question you have about it. Perhaps the best place to talk is a chat session, and I'll update the answer later with all the conclusions we get. Don't know if I can start a chat session with my reputation points, but you surely can. – Christian May 06 '15 at 12:58
  • Thanks but due to timezone difference between Brazil and India it will be difficult to communicate. Appreciate if you can update in answer from your knowledge. – iammilind May 06 '15 at 15:08
  • I updated the answer. Check it out and feel free to make another questions if you want. – Christian May 06 '15 at 17:30
  • @Christian I am also working on GPS for getting current location, but some time i m not able to get the location, calling this method `location = locationMangaer.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)` or `location = locationMangaer.getLastKnownLocation(LocationManager.GPS_PROVIDER);` can u give me some suggestion. – Amjad Khan Oct 27 '17 at 06:01
  • Amjad: try requestLocationUpdates instead. – Christian Oct 27 '17 at 15:57
10

Suggestion is to use Google Location Services It takes the best possible and accurate location as accurate as it can at current moment. It automatically (with configuration of course) takes best current accuracy too - whatever is available GPS, network, internet, GSM/CDMA/LTE... It also cashes last known location so, basically, you know it every moment - the best what you can.

Of course you have to realize that each service provides its own accuracy and in it's own time. Look, for example, GPS Test App on Android and see how accuracy increases with time and used satellites.

Also Location Services is good for you because it simply provides coordinates - just as you asked and hides a lot of work to determine what real service to use based on time and accuracy. However, of course, if none of the services on your particular device and location can give you required accuracy then there is no way to get it. That's why services also provide accuracy measurement.

Here is another link

Community
  • 1
  • 1
Izold Tytykalo
  • 719
  • 1
  • 5
  • 15
  • Do you think these APIs work offline, that's the main requirement to get coordinates. If yes, may you post some code snippets. – iammilind Apr 30 '15 at 02:50
  • By offline you mean no service (Intrnet, WiFi, GSM...)? If there is no service then this particular service is not used. As well as GPS - you can't get GPS signal in the building or underground for example. So, Location Services automatically choose the best known at current moment location. It could be GPS signal one week ago but if it is the best what you can get services will use it. Just take example from link provided and use it. – Izold Tytykalo Apr 30 '15 at 03:05
  • 1
    http://stackoverflow.com/questions/21397177/finding-location-google-play-location-service-or-android-platform-location-api – Izold Tytykalo Apr 30 '15 at 03:16
6

Use fused location API provided by Android SDK.Implement fused location in a Service and call it in your application MainActivity.