Get Latitude and Longitude by GsmCellLocation

Put your problem here if it does not fit any of the other categories.

Get Latitude and Longitude by GsmCellLocation

Postby blindevil » Wed Sep 08, 2010 5:53 pm

Hi, I'm a new Italian User..this is a great forum!! :D
I exopose my problem: I want to get my coordinates by GsmCellLocation. I read this tutorial http://www.anddev.org/poor_mans_gps_-_celltowerid_-_location_area_code_-lookup-t257.html, but the range of my location is about 512 metres. This is my code:

MainActivityClass:
Code: Select all
public class MainActivity extends Activity {


   
private TelephonyManager tm;
private GsmCellLocation location;
private int cid, lac, mcc, mnc, cellPadding, range;

protected CellLocation myPhoneStateReceiver;

private double lat, lng;


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

  /*
   * Setup a listener for the UpdateCellButton. Pressing this button will fetch
   * the current cell info from the phone.
   */
  final Button UpdateCellButton = (Button) findViewById(R.id.UpdateCellButton);
  UpdateCellButton.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
    location = (GsmCellLocation) tm.getCellLocation();
    cid = location.getCid();
    lac = location.getLac();
    getGoogleCellID(lac, cid);
    /*
     * Mcc and mnc is concatenated in the networkOperatorString. The first 3
     * chars is the mcc and the last 2 is the mnc.
     */
    String networkOperator = tm.getNetworkOperator();
    if (networkOperator != null && networkOperator.length() > 0) {
     try {
      mcc = Integer.parseInt(networkOperator.substring(0, 3));
      mnc = Integer.parseInt(networkOperator.substring(3));
     } catch (NumberFormatException e) {
     }
    }

    /*
     * Check if the current cell is a UMTS (3G) cell. If a 3G cell the cell id
     * padding will be 8 numbers, if not 4 numbers.
     */
    if (tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS) {
     cellPadding = 8;
    } else {
     cellPadding = 4;
    }

    /*
     * Update the GUI with the current cell's info
     */
    ((TextView) findViewById(R.id.TextView01)).setText("CellID: "
      + getPaddedHex(cid, cellPadding));
    ((TextView) findViewById(R.id.TextView02)).setText("Lac: "
      + getPaddedHex(lac, 4));
    ((TextView) findViewById(R.id.TextView03)).setText("Mcc: "
      + getPaddedInt(mcc, 3));
    ((TextView) findViewById(R.id.TextView04)).setText("Mnc: "
      + getPaddedInt(mnc, 2));
    ((TextView) findViewById(R.id.TextView06)).setText("Lat: "
             + lat);
    ((TextView) findViewById(R.id.TextView07)).setText("Long: "
             + lng);
    ((TextView) findViewById(R.id.TextView08)).setText("Range: "
           + range);
   


   }
  });
 
  final Button mapButton = (Button) findViewById(R.id.mapButton);
  mapButton.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
         Uri geoURI = Uri.parse("geo:" + lat + "," + lng);
         Intent mapViewIntent = new Intent(android.content.Intent.ACTION_VIEW);
         mapViewIntent.setData(geoURI);
         startActivity(mapViewIntent);
      }
      });

 
 
 
 
}

String getPaddedHex(int nr, int minLen) {
     String str = Integer.toHexString(nr);
     if (str != null) {
      while (str.length() < minLen) {
       str = "0" + str;
      }
     }
     return str;
    }

String getPaddedInt(int nr, int minLen) {
  String str = Integer.toString(nr);
  if (str != null) {
   while (str.length() < minLen) {
    str = "0" + str;
   }
  }
  return str;
}

private void getGoogleCellID(int lac, int cid)
{
   int shortcid = cid & 0xffff;
   
     try
     {
         String surl = "http://www.google.com/glm/mmap";
         
         HttpClient httpclient = new DefaultHttpClient();
         HttpPost httppost = new HttpPost(surl);
         httppost.setEntity(new MyCellIDRequestEntity(shortcid, lac));
         HttpResponse response = httpclient.execute(httppost);
         HttpEntity entity = response.getEntity();
         DataInputStream dis = new DataInputStream(entity.getContent());
           
          // Read some prior data
          dis.readShort();
          dis.readByte();
          // Read the error-code
          int errorCode = dis.readInt();
          if (errorCode == 0)
          {
               lat = (double) dis.readInt() / 1000000D;
               lng = (double) dis.readInt() / 1000000D;
               range = dis.readInt(); // in metres

          }
     } catch (Exception e) {}
}

}


MyCellIDRequestEntity.class
Code: Select all
class MyCellIDRequestEntity implements HttpEntity
{
     protected int myCellID;
     protected int myLAC;

     public MyCellIDRequestEntity(int aCellID, int aLAC)
     {
          this.myCellID = aCellID;
          this.myLAC = aLAC;
     }

     public Header getContentType()
     {
        return new BasicHeader("Content-Type", "application/binary");
     }

   public void consumeContent() throws IOException
   {
   }

   public InputStream getContent() throws IOException, IllegalStateException
   {
      return null;
   }

   public Header getContentEncoding()
   {
      return null;
   }

   public boolean isChunked()
   {
      return false;
   }

   public boolean isStreaming()
   {
      return false;
   }

    /** Pretend to be a French Sony_Ericsson-K750 that
     * wants to receive its lat/long-values =)
     * The data written is highly proprietary !!! */
   public void writeTo(OutputStream outputStream) throws IOException
   {
        DataOutputStream os = new DataOutputStream(outputStream);
        os.writeShort(21);
        os.writeLong(0);
        os.writeUTF("fr");
        os.writeUTF("Sony_Ericsson-K750");
        os.writeUTF("1.3.1");
        os.writeUTF("Web");
        os.writeByte(27);

        os.writeInt(0); os.writeInt(0); os.writeInt(3);
        os.writeUTF("");
        os.writeInt(myCellID); // CELL-ID
        os.writeInt(myLAC); // LAC
        os.writeInt(0); os.writeInt(0);
        os.writeInt(0); os.writeInt(0);
        os.flush();
   }

   @Override
   public long getContentLength()
   {
      return -1;
   }

   @Override
   public boolean isRepeatable()
   {
      return true;
   }


Manifest:
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView android:text="CellID: " android:id="@+id/TextView01"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>

<TextView android:text="Lac: " android:id="@+id/TextView02"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>

<TextView android:text="Mcc: " android:id="@+id/TextView03"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>

<TextView android:text="Mnc: " android:id="@+id/TextView04"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
 
<TextView android:text="Lat: " android:id="@+id/TextView06"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
 
  <TextView android:text="Long: " android:id="@+id/TextView07"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
 
  <TextView android:text="Range: " android:id="@+id/TextView08"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>

<Button android:text="Preleva Info" android:id="@+id/UpdateCellButton"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"/>
 
  <Button android:text="Visualizza in Mappa" android:id="@+id/mapButton"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"/>

</LinearLayout>


Now I have this results:
CellID: f99b0
Lac: c38f
Mcc: 222
Mnc: 99
Lat: 41.908088
Long:12.523084
Range:518

These informations change if change the cell (in the same position). If I want a localization with a minor range what can I do?
I think the better way is to take neighbours cell location (latitude,longidute in the same way) and than do the average? (in this case can you help me to take neibourghs list) or do the average by cellId or lac?
Thanks for help and sorry for my english!
blindevil
Freshman
Freshman
 
Posts: 2
Joined: Wed Sep 08, 2010 5:37 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 22 guests