Reputation: 1622
I am trying to get the current location of my cell phone without using Internet and Gps. I think its possible to get the location using Cell id. I have retrieved the cell id from the phone using the following code. I have no idea how to use the cell id to get latitudes and longitudes. Can anyone suggest me a solution?
My code is as below:
GsmCellLocation location;
int cellID, lac;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
location = (GsmCellLocation) tm.getCellLocation();
cellID = location.getCid();
lac = location.getLac();
Toast.makeText(MainActivity.this,"cellid"+cellID+"loc"+lac,Toast.LENGTH_LONG).show();
}
Upvotes: 4
Views: 4749
Reputation: 78
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
> under onCreate get the Cell information such as MNC MCC CELLID and LAC
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation cellLocation = (GsmCellLocation) telephonyManager.getCellLocation();
tv.setText(String.valueOf(telephonyManager.getPhoneType()));
if(telephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM){
CdmaCellLocation cdmaCellLocation = new CdmaCellLocation();
Toast.makeText(this, "telco location" + cellLocation.toString()
+ "\npsc" + cellLocation.getPsc()
+ "\nlac" + cellLocation.getLac()
+ "\ncid" + cellLocation.getCid()
+ "\n" + cellLocation, Toast.LENGTH_SHORT).show();
}
int cellid = cellLocation.getCid();
int lac = cellLocation.getLac();
String networkOperator = telephonyManager.getNetworkOperator();
if (networkOperator.length() > 0) {
String mcc = networkOperator.substring(0, 3);
String mnc = networkOperator.substring(3);
> call getlocationURL to request to API then it gives you Json
url = getLocationUrl(cellid, lac, mnc, mcc);
System.out.println(cellid + "," + lac + "," + mnc + "," + mcc);
System.out.println(url);
Getloc getloc = new Getloc();
getloc.execute(url);
}
private String getLocationUrl(int cellid, int lac, String mnc, String mcc) {
String url = "http://opencellid.org/cell/get?key=133ee6b1311c65&mcc=" + mcc
+ "&mnc=" + mnc
+ "&lac=" + lac
+ "&cellid=" + cellid
+ "&format=json";
return url;
}
@SuppressLint("StaticFieldLeak")
private class Getloc extends AsyncTask<String, Void, String> {
ProgressDialog pb = new ProgressDialog(MainActivity.this);
@Override
protected void onPreExecute() {
super.onPreExecute();
pb.show();
pb.setMessage("Please wait.....");
System.out.println("Json Data is downloading");
}
@Override
protected String doInBackground(String... url) {
String data = null;
try{
// Fetching the data from web service
data = downloadUrl(url[0]);
}catch(Exception e){
Log.d("Background Task",e.toString());
}
return data;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
pb.dismiss();
String Cheese = "RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRrr"+result;
tv.setText(Cheese);
JSONObject c = null;
try {
c = new JSONObject(String.valueOf(result));
String lat = c.getString("lat");
String lon = c.getString("lon");
Constants.LONGITUDE = lon;
Constants.LATITUDE = lat;
String hakdog = "cellloc"+lat+","+lon;
tv.setText(hakdog);
System.out.println("hahahahahaahahahahahahaha");
} catch (JSONException e) {
e.printStackTrace();
}
}
}
private String downloadUrl(String strUrl) throws IOException{
String data = "";
InputStream iStream = null;
HttpURLConnection urlConnection = null;
try{
URL url = new URL(strUrl);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while( ( line = br.readLine()) != null){
sb.append(line);
}
data = sb.toString();
br.close();
}catch(Exception e){
Log.d("Excep downloading url", e.toString());
}finally{
assert iStream != null;
iStream.close();
urlConnection.disconnect();
}
return data;
}
Upvotes: 0
Reputation: 4869
The cell id does not contain any embedded latitude and longitude value - you simply have to look the id up in a database of ids that does contain those values.
You can build your own database, or use an existing one such at that provided by OpenCellID
Upvotes: 1