Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Location-Based Services Using CellID in Android : Page 2

Though GPS functionality has become hugely popular, it doesn't work indoors and not all devices have built-in GPS receivers. How do you get around this? Learn how to find and use CellID to locate your Android device and use Google maps to display it.


Obtaining the Cell ID and LAC

In Android's SDK 1.0, you can obtain the cell ID of a device using the GsmCellLocation class. In the CellID class, declare the following member variables:

public class CellID extends Activity { GsmCellLocation location; int cellID, lac;

In the onCreate() event handler, create an instance of the TelephonyManager class and use it to obtain an instance of the GsmCellLocation class:

/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); location = (GsmCellLocation) tm.getCellLocation();

Using the instance of the GsmCellLocation class, obtain the device's cell ID and LAC by using its getCid() and getLac() methods, respectively, like this:

cellID = location.getCid(); lac = location.getLac();

Once you've obtained the Cell ID and LAC, you need a way to resolve this information into latitude and longitude in order to display the exact location on a map. To accomplish this, you'll use use a hidden Google Maps API.

Obtaining the Latitude and Longitude using the Google Maps API
Cell IDs are nothing more than strings of numbers; they are useful only when you have a way to translate these numbers into latitude and longitude values. That's where Google Maps comes in handy.

If you've ever used Google Maps for Mobile, you'll be familiar with the My Location feature. This feature allows you to show your current location on the map, even if you don't have GPS on the device. How does Google Maps do this? The trick lies in obtaining the Cell ID contained on your device. My Location obtains the CellID and sends it sent back to a secret API at Google Maps. This API then returns the latitude and longitude that corresponds to the Cell ID. Apparently, Google Maps has a huge database containing all the world's cell IDs (at least it worked pretty well for me in Singapore).

To obtain latitude and longitude information using the cell ID and LAC, open an HTTP connection to the Google Maps API an send it some proprietary data. For this purpose, I have modified two samples previously written by Davanum Srinivas and Nicolas Gramlich.

Author's Note: The two articles referenced above contain code that was written for the older versions of the Android SDK. The code in this article works for the Android SDK 1.0 r1.

Define the displayMap() function within the CellID class as shown in Listing 1.

Basically, you open a connection to the Google Maps API using a HTTP connection. You then POST some proprietary data to the server using the WriteData() function (to be defined next). When the API responds with some data, you extract the part containing the latitude and longitude of the location. With the latitude and longitude obtained, you can now launch Google Maps to display the location.

Note that this method of resolving Cell ID and LAC information to latitude and longitude is not supported by Google. Chances are that Google may decide to remove this feature anytime they realize that it is being misused. Define the WriteData() function as shown in Listing 2. In particular, note the place where you insert the cell ID and LAC values.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.