Mar 19 2012

Understanding Android: Three Ways to Find Your Location

It’s not all GPS.  How exactly does an Android device obtain your current location?  I had to hunt the webs for each answer separately, so for users and developers alike this article quickly highlights the three ways Android discovers where you are.  In order of power consumption, Cell-ID, Wi-Fi, and GPS can be powerful tools.  Now let’s begin.

hand holding a globe

globe by Judy **


How it works: You probably already know that your phone is virtually always connected to the nearest cell tower.  Each cell tower’s exact location is known by its operators and is assigned to the tower as a Cell ID within a Location Area Code (LAC).  Your phone stores the Cell ID and LAC of every tower to which you connect, giving it a rough idea of your location. To handle the higher densities of people using up tower bandwidth in urban areas, operators build more towers that each cover smaller areas.  Therefore, in dense cities like Los Angeles, the Cell ID can actually be fairly accurate.  In general, though, not so much.  Note: Cell IDs are actually codes that must be looked up in secure databases to find the associated latitude and longitude.  There are public projects that aim to catalog these Cell IDs for average user look-up, but in general, no one can simply read the Cell ID itself and know where you are.

Power usage: Since the mobile network is the core service of any phone, requesting your location by Cell-ID requires very little extra load in terms of processing and power consumption.  If you’re worried about draining the battery, this is the best option to start with.

Accuracy: Cell-ID is notoriously inaccurate, but I’ve occasionally achieved GPS-level accuracy using it with Wi-Fi and GPS disabled.  It largely depends on where you are and how long you ask it to keep requesting updates.  I wouldn’t depend on it alone under normal circumstances.

How to use it in your app: The long way is the first one you’ll find when searching for Android Cell-ID. This involves creating an instance of TelephonyManager to acquire the Cell-ID, then routing it through a super-secret-special Google Maps API for which a Java code routine has been widely published online.  But forget all that: what few sites mention is that the standard requestLocationUpdates() called on NETWORK_PROVIDER already uses your Cell-ID to obtain your location so long as you have your mobile network enabled.  See the end of the post for a code example.

a bench advertising free wifi

Free WiFi by xcode


How it works: Unless you opt out, your phone is periodically sending anonymous data to Google with, among other things, your last known location and any Wi-Fi network you were connected to at the time.  The accumulated data builds on a database begun by traveling Google Streetview cars that recorded Wi-Fi networks available along their routes (the cars no longer do this).  So, like the Cell ID database, your current Wi-Fi networks can now be cross-referenced to find your approximate location.  By default, Android overlays this information with your Cell-ID location range to get a fairly accurate idea of where you are.

Power usage: Wi-Fi runs on a separate chip in your Android device, likely a chip that handles both Wi-Fi and Bluetooth communications simultaneously.  This chip only draws power when in use, but it draws considerably more power than your phone uses under normal operation.  Although each location request only runs for a matter of seconds, these bursts of extra power usage add up over time.  On the plus side, you likely already have Wi-Fi enabled (like your mobile network), and Wi-Fi uses less power than GPS.

Accuracy: This service depends on having available Wi-Fi networks and whether those Wi-Fi networks have been previously catalogued.  If those conditions are satisfied, you may get a nicely accurate result.  It is best (as Google Maps will inform you) to use Cell-ID and Wi-Fi together so each can cover the other’s weak spots.

How to use it in your app: Code-wise, Wi-Fi-based location is obtained alongside Cell-ID location through a requestLocationUpdates() called on NETWORK_PROVIDER.  I haven’t been able to find a way to get the location from Wi-Fi only, but I wouldn’t really want to.  Disable the mobile network before calling this if you really need to limit it.  For the code example, see the end of the post.

Rendering of satellite in space

ROSAT satellite by YU-bin


How it works: Google “how does GPS work” to find the details of three-dimensional trilateration.  Simply put, your Android device requests its distance from satellites in the GPS network and plots the intersection points along the boundary of the described spheres.  The more satellites respond, the more accurate your estimated location.

Power usage: Like Wi-Fi, GPS runs as an independently-powered component.  Since we don’t use GPS for data-transfer, it largely sits dormant – this is good, since it eats through power like a streaming high-definition video.  Use it sparingly and only if you need great precision.

Accuracy: Like any location service, GPS is dependent on your surroundings for its accuracy.  Unlike the others, it is capable of placing you within feet of your exact location and even includes an estimation of your elevation.  Although typically best when out-of-doors, GPS on modern phones and tablets can still be extremely accurate in moving cars and in multi-level buildings.

How to use it in your app: Requesting GPS-only locations is simple in Android.  Call requestLocationUpdates() on GPS_PROVIDER.  See below for a code example.


//The basics for Android SDK location retrieval.

//Declare class variables in this example.
private static LocationManager locationManager;
private static LocationListener locationListener;

//Instantiate when ready to use.
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
  public void onLocationChanged(Location location) {
    //What to do for each location "fix."
  public void onStatusChanged(String provider, int status, Bundle extras) {
    //What to do if, say, the user loses or leaves the service area.
  public void onProviderEnabled(String provider) {
    //What to do if Mobile, WiFi, or GPS are enabled during process.
  public void onProviderDisabled(String provider) {
    //What to do if Mobile, WiFi, or GPS are disabled during proccess.

//Cell-ID and WiFi location updates.
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
//GPS location updates.
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

//Refer to the Android Developers resources for more.

Whether you’re a developer or you’re just curious about how Android goes about getting your location, I hope this saved you some hunting and answered your questions.

Any tips or information to add?  Comment and share to keep the discussion going.  And subscribe via Email or by RSS if you like this blog!


5 pings

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>