1

如何通过 LAC 和 CELLID 获取经度和纬度?

4

1 回答 1

0

使用以下代码获取 lat 和 lng 以使用 cid 和 lac。

    private Boolean findLocation(int cid, int lac) {
    Boolean result = false;

    String urlmmap = "http://www.google.com/glm/mmap";
    // Set-up proxy
    Properties systemProperties = System.getProperties();
    systemProperties.setProperty("http.proxyHost", urlmmap);
    //systemProperties.setProperty("http.proxyPort", String.valueOf(port));
    systemProperties.setProperty("http.keepAlive", "false");

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

public class NetworkAsync extends AsyncTask<Void,Void,String>
{
    int cid, lac, dbm;
    ProgressDialog progressDialog;
    public NetworkAsync(int cid, int lac, int dbm)
    {
        this.cid = cid;
        this.lac = lac;
        this.dbm = dbm;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = new ProgressDialog(DashboardActivity.this);
        progressDialog.show();
    }

    @Override
    protected String doInBackground(Void... voids) {
        if(findLocation(cid, lac)){
            latlng=
                    String.valueOf((float)myLatitude/1000000)
                            + " : "
                            + String.valueOf((float)myLongitude/1000000);
            Log.d("latlong",latlng);
            latLngList.add(latlng+" : "+dbm);
        }else{
            latlng = "Can't find Location!";
            Log.d("latlong",latlng);
        }
        return latlng;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        progressDialog.dismiss();
    }
}

在您想调用上述方法的地方使用下面的代码,我的意思是在按钮单击或 onResume() 等中。

NetworkAsync networkAsync = new NetworkAsync(location.getCid(),location.getLac(),-81);
networkAsync.execute();
于 2019-09-20T05:59:01.857 回答