"http://www.google.com/glm/mmap" is a non-public API to convert cellLocation to latitude and longitude.
Make a project with name LocationWithoutGPS and have a main activity with name AndroidTelephonyManager.java with following code
package com.JITESH.locationwithoutgps;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.widget.TextView;
public class AndroidTelephonyManager extends Activity {
int myLatitude, myLongitude;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textGsmCellLocation = (TextView)findViewById(R.id.gsmcelllocation);
TextView textCID = (TextView)findViewById(R.id.cid);
TextView textLAC = (TextView)findViewById(R.id.lac);
TextView textGeo = (TextView)findViewById(R.id.geo);
//retrieve a reference to an instance of TelephonyManager
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation cellLocation = (GsmCellLocation) telephonyManager.getCellLocation();
int cid = cellLocation.getCid();
int lac = cellLocation.getLac();
textGsmCellLocation.setText(cellLocation.toString());
textCID.setText("gsm cell id: " + String.valueOf(cid));
textLAC.setText("gsm location area code: " + String.valueOf(lac));
if(RqsLocation(cid, lac)){
textGeo.setText(
String.valueOf((float)myLatitude/1000000)
+ " : "
+ String.valueOf((float)myLongitude/1000000));
}else{
textGeo.setText("Can't find Location!");
}
}
private Boolean RqsLocation(int cid, int lac){
Boolean result = false;
String urlmmap = "http://www.google.com/glm/mmap";
try {
URL url = new URL(urlmmap);
URLConnection conn = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.connect();
OutputStream outputStream = httpConn.getOutputStream();
WriteData(outputStream, cid, lac);
InputStream inputStream = httpConn.getInputStream();
DataInputStream dataInputStream = new DataInputStream(inputStream);
dataInputStream.readShort();
dataInputStream.readByte();
int code = dataInputStream.readInt();
if (code == 0) {
myLatitude = dataInputStream.readInt();
myLongitude = dataInputStream.readInt();
result = true;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
private void WriteData(OutputStream out, int cid, int lac)
throws IOException
{
DataOutputStream dataOutputStream = new DataOutputStream(out);
dataOutputStream.writeShort(21);
dataOutputStream.writeLong(0);
dataOutputStream.writeUTF("en");
dataOutputStream.writeUTF("Android");
dataOutputStream.writeUTF("1.0");
dataOutputStream.writeUTF("Web");
dataOutputStream.writeByte(27);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(3);
dataOutputStream.writeUTF("");
dataOutputStream.writeInt(cid);
dataOutputStream.writeInt(lac);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.flush();
}
}
the main.xml has following layout
Make a project with name LocationWithoutGPS and have a main activity with name AndroidTelephonyManager.java with following code
package com.JITESH.locationwithoutgps;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.widget.TextView;
public class AndroidTelephonyManager extends Activity {
int myLatitude, myLongitude;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textGsmCellLocation = (TextView)findViewById(R.id.gsmcelllocation);
TextView textCID = (TextView)findViewById(R.id.cid);
TextView textLAC = (TextView)findViewById(R.id.lac);
TextView textGeo = (TextView)findViewById(R.id.geo);
//retrieve a reference to an instance of TelephonyManager
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation cellLocation = (GsmCellLocation) telephonyManager.getCellLocation();
int cid = cellLocation.getCid();
int lac = cellLocation.getLac();
textGsmCellLocation.setText(cellLocation.toString());
textCID.setText("gsm cell id: " + String.valueOf(cid));
textLAC.setText("gsm location area code: " + String.valueOf(lac));
if(RqsLocation(cid, lac)){
textGeo.setText(
String.valueOf((float)myLatitude/1000000)
+ " : "
+ String.valueOf((float)myLongitude/1000000));
}else{
textGeo.setText("Can't find Location!");
}
}
private Boolean RqsLocation(int cid, int lac){
Boolean result = false;
String urlmmap = "http://www.google.com/glm/mmap";
try {
URL url = new URL(urlmmap);
URLConnection conn = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.connect();
OutputStream outputStream = httpConn.getOutputStream();
WriteData(outputStream, cid, lac);
InputStream inputStream = httpConn.getInputStream();
DataInputStream dataInputStream = new DataInputStream(inputStream);
dataInputStream.readShort();
dataInputStream.readByte();
int code = dataInputStream.readInt();
if (code == 0) {
myLatitude = dataInputStream.readInt();
myLongitude = dataInputStream.readInt();
result = true;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
private void WriteData(OutputStream out, int cid, int lac)
throws IOException
{
DataOutputStream dataOutputStream = new DataOutputStream(out);
dataOutputStream.writeShort(21);
dataOutputStream.writeLong(0);
dataOutputStream.writeUTF("en");
dataOutputStream.writeUTF("Android");
dataOutputStream.writeUTF("1.0");
dataOutputStream.writeUTF("Web");
dataOutputStream.writeByte(27);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(3);
dataOutputStream.writeUTF("");
dataOutputStream.writeInt(cid);
dataOutputStream.writeInt(lac);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.flush();
}
}
the main.xml has following layout
<?
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:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"@string/hello"
/>
<
TextView
android:id
=
"@+id/gsmcelllocation"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
/>
<
TextView
android:id
=
"@+id/cid"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
/>
<
TextView
android:id
=
"@+id/lac"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
/>
<
TextView
android:id
=
"@+id/geo"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
/>
</
LinearLayout
>
the manifest has following permissions
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Doesn't work now. Last value returns -1.
ReplyDeleteHi, please investigate the logs, if possible please sen them.
DeleteHi Jitesh, I am implementing this same concept using webpage. So, could you please tell me how can I manipulate the same code if I am providing CID and LAC and want to get location coordinates from that??
ReplyDeleteHi, as you wrote "I am implementing this same concept using webpage" i am not able to understand it clearly, please elaborate and explain the conditions!!
DeleteThis comment has been removed by the author.
ReplyDeleteI am getting value of code = 1, where it should be 0 for the result to be true..any help?
ReplyDelete