我试图在不使用互联网和Gps的情况下获取手机的当前位置。我认为可以使用Cell id获取位置。我使用以下代码从手机中检索了手机信号。我不知道如何使用cell id来获得纬度和经度。有谁能建议我解决方案?
我的代码如下:
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();
}
答案 0 :(得分:0)
单元格ID不包含任何嵌入的纬度和经度值 - 您只需在包含这些值的ID数据库中查找id。
您可以构建自己的数据库,也可以使用OpenCellID
提供的现有数据库答案 1 :(得分:0)
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;
}