LBS for non GPS device through Android

[Android] Oct 27, 2011


About This Article

The GPS functionality has become highly popular, but on device it has some constrain it does not work indoors and not all devices having built-in GPS receiver. This document describes how to get the latitude and longitude for non GPS can cell phone using Open Cell-ID, followed by sample code.


This document gives introduction about retrieving latitude and longitude on Android cell phone. It assumes that user has already install Android and necessary tools to develop application. It also assumes that the user to be familiar with Basic knowledge of Android


A GSM Cell ID (CID) is a generally unique number used to identify each Base transceiver station (BTS) or sector of a BTS within a Location area code (LAC) if not within a GSM network.

User's phone is always connected to a cellular network, and by knowing CellID number, we know the Cell, and by knowing the position of the cell, we know where user is. There is some accuracy issue’s, as the cell can cover from several hundreds of meters to several kilometers, but this could be a very good starting point to locate user.


To get the current location using cell-id we need to access telephony services of the device which confer by TelephonyManager class. In Android CELL_ID, Wi-Fi etc. are permission protected services to access this information we need to add uses permission
"android.permission.ACCESS_COARSE_LOCATION" into the manifest.xml file, it allows to access coarse location.


<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>

Initialize Telephony Services

After adding the uses-permission into the manifest.xml we need to initialize the telephony services by taking the reference of TelephonyManager through Context.getSystemService(String)


telephoneyMg = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);


TELEPHONY_SERVICE Use with getSystemService(String) to retrieve a TelephonyManager for handling management the telephony features of the device.

Get Cell-ID & LAC

To get the GSM location of the device we need to take reference of GsmCellLocation, it gives access to cell-id & lac.


gsmCellLocation = (GsmCellLocation) telephoneyMg.getCellLocation();
iCellID = gsmCellLocation.getCid();
iLAC = gsmCellLocation.getLac();


If cell-id and lac max value is belong 0 to 0xffff then its legal otherwise unknown.

Getting cell position

To get cell position (latitude and longitude) using cell-id & lac we need to do http connection, to access http connection service we need to add internet permission into the manifest.xml file.


<uses-permission android:name="android.permission.INTERNET"></uses-permission>


After adding internet permission now we can fetch latitude & longitude using http connection


String urlmmap = "";

	URL url = new URL(urlmmap);		
	URLConnection conn = url.openConnection();	
	HttpURLConnection httpConn = (HttpURLConnection) conn;	


	OutputStream outputStream = httpConn.getOutputStream();		
	WriteData(outputStream, iCellID, iLac);//user method	
	InputStream inputStream = httpConn.getInputStream();
	DataInputStream dataInputStream = new DataInputStream(inputStream);



	int code = dataInputStream.readInt();		
	if(code == 0){		
		mLatitude = dataInputStream.readInt() / 1000000D;//latitude 
		mLongitude = dataInputStream.readInt() / 1000000D;//longitude 


Getting cell position

Once we get latitude and longitude now we are ready to launch the google map as a separate activity. This code snippet displays the current position of cell.


String uri = "geo:" + httpConn.getLatitude() + ","+ httpConn.getLongitude() + "?z=" + 10;
startActivity(new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri)));

Sample Code Snippet

The following code snippet demonstrates how to get latitude and longitude using open cell-id.

Class: AndroidCellID


import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View.OnClickListener;
import android.content.Intent;

public class AndroidCellID extends Activity implements OnClickListener,
AsyncCallBack {

	public final int CELL_ID = 1;
	public final int HTTPCONN = 2;
	private Button mButtonUpdate;
	private Button mButtonLocateMe;
	private TextView mTextViewCell_ID;
	private TextView mTextViewLAC;
	private EditText mEditTextCell_ID;
	private EditText mEditTextLAC;
	private GetCellID mCellId;
	private HttpConn httpConn;
	private ProgressDialog progressDialog;

	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {


	private void init() {		

		mButtonUpdate = (Button) findViewById(;
		mButtonLocateMe = (Button) findViewById(;
		mTextViewCell_ID = (TextView) findViewById(;
		mTextViewLAC = (TextView) findViewById(;		
		mEditTextCell_ID = (EditText) findViewById(;
		mEditTextLAC = (EditText) findViewById(;
		mCellId = new GetCellID(this, CELL_ID, this);
		progressDialog = new ProgressDialog(this);
		progressDialog.setMessage("Featching info...");		

	public void onClick(View v) {

		int id = v.getId();

		switch (id) {

			String cellid = ("" + mEditTextCell_ID.getEditableText()).trim();
			String lac = ("" + mEditTextLAC.getEditableText()).trim();
			if ((!cellid.equals("-1")) && (!lac.equals("-1"))) {;
				httpConn = new HttpConn(cellid, lac, this, HTTPCONN);
		Toast.makeText(this,"update for cell id!!!", Toast.LENGTH_LONG).show();
	public void updateStatus(boolean f, int id) {
		switch (id) {
		case CELL_ID:
			if (mCellId != null) {
				mEditTextCell_ID.setText("" + mCellId.getCellID());
				mEditTextLAC.setText("" + mCellId.getLAC());
		case HTTPCONN:
			if (f == true) {
				if (httpConn != null) {
				String uri = "geo:" + httpConn.getLatitude() + ","
				+ httpConn.getLongitude() + "?z=" + 10;
				startActivity(new Intent(	android.content.Intent.ACTION_VIEW, Uri.parse(uri)));
				//Unable to found cell id
				Toast.makeText(this,"Unable to found cell id", Toast.LENGTH_LONG).show();
			} else {
				//http connection fail
				Toast.makeText(this,"Http Connection fail", Toast.LENGTH_LONG).show();

COPY URL twitter facebook