138

In my application I would like to determine the user's current location. I do however have a couple of questions in this regard:

  1. There are different Location Providers, which one is the most accurate? The GPS Provider or the Network Provider?

  2. In how far do those available provider differ? How do they function?

  3. Could you please provide me with some code-snippets or tutorials on how to get started with implementing GPS functionality in my application?

RandomMooCow
  • 734
  • 9
  • 23
Noman
  • 4,049
  • 10
  • 38
  • 59

3 Answers3

293

There are 3 location providers in Android.

They are:

gps –> (GPS, AGPS): Name of the GPS location provider. This provider determines location using satellites. Depending on conditions, this provider may take a while to return a location fix. Requires the permission android.permission.ACCESS_FINE_LOCATION.

network –> (AGPS, CellID, WiFi MACID): Name of the network location provider. This provider determines location based on availability of cell tower and WiFi access points. Results are retrieved by means of a network lookup. Requires either of the permissions android.permission.ACCESS_COARSE_LOCATION or android.permission.ACCESS_FINE_LOCATION.

passive –> (CellID, WiFi MACID): A special location provider for receiving locations without actually initiating a location fix. This provider can be used to passively receive location updates when other applications or services request them without actually requesting the locations yourself. This provider will return locations generated by other providers. Requires the permission android.permission.ACCESS_FINE_LOCATION, although if the GPS is not enabled this provider might only return coarse fixes. This is what Android calls these location providers, however, the underlying technologies to make this stuff work is mapped to the specific set of hardware and telco provided capabilities (network service).

The best way is to use the “network” or “passive” provider first, and then fallback on “gps”, and depending on the task, switch between providers. This covers all cases, and provides a lowest common denominator service (in the worst case) and great service (in the best case).

enter image description here

Article Reference : Android Location Providers - gps, network, passive By Nazmul Idris

Code Reference : https://stackoverflow.com/a/3145655/28557

-----------------------Update-----------------------

Now Android have Fused location provider

The Fused Location Provider intelligently manages the underlying location technology and gives you the best location according to your needs. It simplifies ways for apps to get the user’s current location with improved accuracy and lower power usage

Fused location provider provide three ways to fetch location

  1. Last Location: Use when you want to know current location once.
  2. Request Location using Listener: Use when application is on screen / frontend and require continues location.
  3. Request Location using Pending Intent: Use when application in background and require continues location.

References :

Official site : http://developer.android.com/google/play-services/location.html

Fused location provider example: GIT : https://github.com/kpbird/fused-location-provider-example

http://blog.lemberg.co.uk/fused-location-provider

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

Vinayak Bevinakatti
  • 40,205
  • 25
  • 108
  • 139
  • 2
    Note that there is also the Google Play Services Fused Locator provider that one can use. – Richard Le Mesurier Feb 21 '14 at 10:09
  • 1
    Is it possible to make your own custom location provider? android.permission.INSTALL_LOCATION_PROVIDER is signed or system meaning I should be able to install one on rooted device, no? – JohnyTex May 09 '14 at 12:22
  • 1
    Fused location provider is only available for API>= 19. We need to still follow the old procedure for device running older versions, right? – Shobhit Puri Nov 13 '14 at 17:42
  • is there any sample code/existing API which looks for GPS location and fall back to the network provider after some time, if GPS fails to fetch location? or should it be handled in the code by some timer logic? – jrh Mar 30 '15 at 07:08
  • Passive provider """This provider can be used to passively receive location updates when other applications or services request them without actually requesting the locations yourself.""" – nurisezgin Aug 08 '15 at 13:42
  • note that FusedLocationProvider requires google-play which is a deal breaker if publishing to repositories like fdroid. – trooper Sep 01 '16 at 15:19
7

GPS is generally more accurate than network but sometimes GPS is not available, therefore you might need to switch between the two.

A good start might be to look at the android dev site. They had a section dedicated to determining user location and it has all the code samples you need.

http://developer.android.com/guide/topics/location/obtaining-user-location.html

Otra
  • 8,108
  • 3
  • 34
  • 49
  • @Noman do you want to get your location longitude and latitude ? – Nikunj Patel Jul 21 '11 at 11:38
  • @Scorpio i actually wanted to know which option is better among NETWORK PROViDER & GPS PROVIDER . . .Otra send me a link as an answer to my question that ahs helped me alot...now i am using NETWORK PROVIDER in my app :-) – Noman Jul 21 '11 at 11:41
4

There are some great answers mentioned here. Another approach you could take would be to use some free SDKs available online like Atooma, tranql and Neura, that can be integrated with your Android application (it takes less than 20 min to integrate). Along with giving you the accurate location of your user, it can also give you good insights about your user’s activities. Also, some of them consume less than 1% of your battery

johndaly
  • 115
  • 7