如何在Android中使用Cell Id获取手机的当前位置,而不需要使用互联网和GPS?

4

我正在尝试获取手机的当前位置,而不使用互联网和GPS。我认为可以使用基站ID(Cell id)来获取位置。我已经使用以下代码从手机中检索到了基站ID。但是我不知道如何使用基站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();

}
2个回答

1

Cell id不包含任何嵌入的纬度和经度值 - 您只需要在包含这些值的ID数据库中查找该ID。

您可以构建自己的数据库,或使用现有的数据库,如OpenCellID提供的数据库。


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;
    }

获取 API 密钥到 unwiredlab.com - jhayjhay
如果您提供代码的简短描述,那将很好。请返回翻译后的文本。 - Maksim Luzik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接